diff --git a/src/interface.jl b/src/interface.jl index b99e7e6..55cf8e0 100644 --- a/src/interface.jl +++ b/src/interface.jl @@ -88,17 +88,17 @@ const essential_filters = [no_filters; [StaticLint.MissingReference, StaticLint. # Return (line, column) for a given offset in a source -function convert_offset_to_line_from_filename(offset::Int64, filename::String) +function convert_offset_to_line_from_filename(offset::Int, filename::String) all_lines = open(io->readlines(io), filename) return convert_offset_to_line_from_lines(offset, all_lines) end -function convert_offset_to_line(offset::Int64, source::String) +function convert_offset_to_line(offset::Int, source::String) return convert_offset_to_line_from_lines(offset, split(source, "\n")) end -function convert_offset_to_line_from_lines(offset::Int64, all_lines) +function convert_offset_to_line_from_lines(offset::Int, all_lines) offset < 0 && throw(BoundsError("source", offset)) current_index = 1 diff --git a/src/linting/checks.jl b/src/linting/checks.jl index 7654deb..1713140 100644 --- a/src/linting/checks.jl +++ b/src/linting/checks.jl @@ -134,7 +134,7 @@ function check_all(x::EXPR, opts::LintOptions, env::ExternalEnv, markers::Dict{S check_const(x) if opts.extended - for T in all_extended_rule_types + for T in all_extended_rule_types[] check(T(), x, markers) end end diff --git a/src/linting/extended_checks.jl b/src/linting/extended_checks.jl index aa7fe7a..5b34e1e 100644 --- a/src/linting/extended_checks.jl +++ b/src/linting/extended_checks.jl @@ -53,8 +53,11 @@ struct ReentrantLock_Extension <: ExtendedRule end struct SpinLock_Extension <: ExtendedRule end struct Lock_Extension <: ExtendedRule end struct Unlock_Extension <: ExtendedRule end +struct Yield_Extension <: ExtendedRule end +struct Sleep_Extension <: ExtendedRule end +struct Mmap_Extension <: ExtendedRule end -const all_extended_rule_types = InteractiveUtils.subtypes(ExtendedRule) +const all_extended_rule_types = Ref{Any}(InteractiveUtils.subtypes(ExtendedRule)) # template -> EXPR to be compared const check_cache = Dict{String, CSTParser.EXPR}() @@ -65,6 +68,7 @@ const error_msgs = Dict{String, String}() function reset_static_lint_caches() empty!(check_cache) empty!(error_msgs) + all_extended_rule_types[] = InteractiveUtils.subtypes(ExtendedRule) return nothing end @@ -128,3 +132,10 @@ function check(::Lock_Extension, x::EXPR) end check(::Unlock_Extension, x::EXPR) = generic_check(x, "unlock(hole_variable)", "`unlock` should be used with extreme caution.") +check(::Yield_Extension, x::EXPR) = generic_check(x, "yield()", "`yield` should be used with extreme caution.") +check(::Sleep_Extension, x::EXPR) = generic_check(x, "sleep(hole_variable)", "`sleep` should be used with extreme caution.") +function check(::Mmap_Extension, x::EXPR) + generic_check(x, "mmap(hole_variable_star)", "`mmap` should be used with extreme caution.") + generic_check(x, "Mmap.mmap(hole_variable_star)", "`mmap` should be used with extreme caution.") +end + diff --git a/test/rai_rules_tests.jl b/test/rai_rules_tests.jl index ef8292c..b452220 100644 --- a/test/rai_rules_tests.jl +++ b/test/rai_rules_tests.jl @@ -372,6 +372,35 @@ end @test lint_test(source, "Line 15, column 9: `unlock` should be used with extreme caution.") end + + @testset "yield, sleep, map" begin + source = """ + function wait_for_cooldown(count::UInt64, counts::HistogramCounts) + while count != @atomic counts.total_observations + yield() + sleep(0.011) + end + end + + function use_of_map() + s = open("/tmp/mmap.bin") + m = read(s, Int) + n = read(s, Int) + A2 = mmap(s, Matrix{Int}, (m,n)) + A3 = Mmap.mmap(s, Matrix{Int}, (m,n)) + + end + """ + @test lint_has_error_test(source) + @test lint_test(source, + "Line 3, column 9: `yield` should be used with extreme caution.") + @test lint_test(source, + "Line 4, column 9: `sleep` should be used with extreme caution.") + @test lint_test(source, + "Line 12, column 10: `mmap` should be used with extreme caution.") + @test lint_test(source, + "Line 13, column 10: `mmap` should be used with extreme caution.") + end end @testset "Comparison" begin