From b56355c9b3aebf453b8b81d52a52e94b515f36e0 Mon Sep 17 00:00:00 2001 From: Alexandre Bergel Date: Thu, 7 Mar 2024 12:11:27 +0100 Subject: [PATCH] Adding in, hashkey, equal --- src/linting/extended_checks.jl | 20 ++++++++++++++++++++ test/rai_rules_tests.jl | 27 +++++++++++++++++++++++++++ 2 files changed, 47 insertions(+) diff --git a/src/linting/extended_checks.jl b/src/linting/extended_checks.jl index 7b29b36..c1dc150 100644 --- a/src/linting/extended_checks.jl +++ b/src/linting/extended_checks.jl @@ -71,6 +71,10 @@ struct Channel_Extension <: ExtendedRule end struct Task_Extension <: ExtendedRule end struct ErrorException_Extension <: ExtendedRule end struct Error_Extension <: ExtendedRule end +struct In_Extension <: ExtendedRule end +struct HasKey_Extension <: ExtendedRule end +struct Equal_Extension <: ExtendedRule end + const all_extended_rule_types = Ref{Any}(InteractiveUtils.subtypes(ExtendedRule)) @@ -217,3 +221,19 @@ function check(::Error_Extension, x::EXPR) "Use custom exception instead of the generic `error(...)`") end +function check(::In_Extension, x::EXPR) + msg = "It is preferable to use `tin(item,collection)` instead of the Julia's `in`." + generic_check(x, "in(hole_variable,hole_variable)", msg) + generic_check(x, "hole_variable in hole_variable", msg) + +end + +function check(::HasKey_Extension, x::EXPR) + msg = "It is preferable to use `thaskey(dict,key)` instead of the Julia's `haskey`." + generic_check(x, "haskey(hole_variable,hole_variable)", msg) +end + +function check(::Equal_Extension, x::EXPR) + msg = "It is preferable to use `tequal(dict,key)` instead of the Julia's `equal`." + generic_check(x, "equal(hole_variable,hole_variable)", msg) +end \ No newline at end of file diff --git a/test/rai_rules_tests.jl b/test/rai_rules_tests.jl index d02dd12..b8e589d 100644 --- a/test/rai_rules_tests.jl +++ b/test/rai_rules_tests.jl @@ -4,6 +4,9 @@ import CSTParser using Test using JSON3 +# Reset the caches before running the tests. +StaticLint.reset_static_lint_caches() + function foo() @async 1 + 2 end @@ -461,6 +464,26 @@ end "Line 2, column 9: Need a specific Array type to be provided.", directory = "src/Compiler/") end + + @testset "in, equal, haskey" begin + source = """ + function f() + x = 10 in [10] + y = in(10, [10]) + z = equal(10, "hello") + w = haskey(Dict(1=>1000), 1) + end + """ + @test lint_has_error_test(source) + @test lint_test(source, + "Line 2, column 9: It is preferable to use `tin(item,collection)` instead of the Julia's `in`.") + @test lint_test(source, + "Line 3, column 9: It is preferable to use `tin(item,collection)` instead of the Julia's `in`.") + @test lint_test(source, + "Line 4, column 9: It is preferable to use `tequal(dict,key)` instead of the Julia's `equal`.") + @test lint_test(source, + "Line 5, column 9: It is preferable to use `thaskey(dict,key)` instead of the Julia's `haskey`.") + end end @testset "Comparison" begin @@ -512,6 +535,10 @@ end @test t("Future{T}(()->nothing)", "Future{hole_variable}(hole_variable_star)") @test !t("Future()", "Future{hole_variable}(hole_variable_star)") @test !t("Future{Any}() do f nothing end", "Future{hole_variable}(hole_variable_star)") + + # in keyword + @test t("in(hole_variable,hole_variable)", "in(x,y)") + @test t("x in y", "hole_variable in hole_variable") end @testset "offset to line" begin