From 7b3c19c93f662d596ddffc2fa43a6163b8fa982f Mon Sep 17 00:00:00 2001 From: Alexandre Bergel Date: Mon, 8 Jul 2024 15:29:11 +0200 Subject: [PATCH] Ignoring backquote in local disabling --- src/linting/extended_checks.jl | 28 ++++++++++++++++++++-------- test/rai_rules_tests.jl | 26 ++++++++++++++++++++++++++ 2 files changed, 46 insertions(+), 8 deletions(-) diff --git a/src/linting/extended_checks.jl b/src/linting/extended_checks.jl index 0abed42..2937a81 100644 --- a/src/linting/extended_checks.jl +++ b/src/linting/extended_checks.jl @@ -222,16 +222,28 @@ function reset_static_lint_caches() end function retrieve_full_msg_from_prefix(msg_prefix::String) - the_keys = collect(keys(StaticLint.is_recommendation)) - is = findall(startswith(msg_prefix), the_keys) + function _tmp(msg::String, should_remove_backquote::Bool) + the_keys = collect(keys(StaticLint.is_recommendation)) + if should_remove_backquote + the_keys = map(l->replace(l, "`" => ""), the_keys) + end + is = findall(startswith(msg_prefix), the_keys) + + length(is) == 0 && return is + + if length(is) != 1 + isdefined(Main, :Infiltrator) && Main.infiltrate(@__MODULE__, Base.@locals, @__FILE__, @__LINE__) + end + @assert length(is) == 1 + return the_keys[first(is)] + end - length(is) == 0 && return is + isdefined(Main, :Infiltrator) && Main.infiltrate(@__MODULE__, Base.@locals, @__FILE__, @__LINE__) + t = _tmp(msg_prefix, false) + isempty(t) || return t - if length(is) != 1 - isdefined(Main, :Infiltrator) && Main.infiltrate(@__MODULE__, Base.@locals, @__FILE__, @__LINE__) - end - @assert length(is) == 1 - return the_keys[first(is)] + t = _tmp(replace(msg_prefix, "`" => ""), true) + return t end function get_recommendation(msg_prefix) diff --git a/test/rai_rules_tests.jl b/test/rai_rules_tests.jl index 75f9418..bcc9b07 100644 --- a/test/rai_rules_tests.jl +++ b/test/rai_rules_tests.jl @@ -1405,6 +1405,21 @@ end @test !lint_has_error_test(source) end + + + @testset "Locally disabling rule 06: ignoring backquotes" begin + source = """ + function f() + # lint-disable-next-line: Macro @spawn should be used instead of @async. + @async 1 + 1 + end + """ + source_lines = split(source, "\n") + @test convert_offset_to_line_from_lines(30, source_lines) == (2, 17, nothing) + @test convert_offset_to_line_from_lines(97, source_lines) == (3, 6, "lint-disable-line Macro @spawn should be used instead of @async.") + + @test !lint_has_error_test(source) + end end @testset "Relaxing unused bindings" begin @@ -1530,9 +1545,20 @@ end @test !rule_is_violation("`@lock` should be used with extreme caution.") @test !rule_is_violation("`@lock` ") + # Ignoring backquoting + @test rule_is_violation("Macro @spawn should be used instead of `@async.") + @test rule_is_violation("Macro @spawn") + @test !rule_is_violation("@lock should be used with extreme caution.") + @test !rule_is_violation("`@lock ") + @test rule_is_recommendation("Splatting (`...`) should be used with extreme caution.") + # Ignoring backquoting + @test StaticLint.retrieve_full_msg_from_prefix("@lock ") == + "`@lock` should be used with extreme caution." + + @test StaticLint.retrieve_full_msg_from_prefix("`@lock` ") == "`@lock` should be used with extreme caution." @test StaticLint.retrieve_full_msg_from_prefix("Macro `@spawn`") ==