Skip to content

Commit

Permalink
Merge pull request #29 from RelationalAI/adding-hash-in
Browse files Browse the repository at this point in the history
Adding in, hashkey, equal
  • Loading branch information
bergel authored Mar 7, 2024
2 parents 47272b6 + b56355c commit 2607c86
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 0 deletions.
20 changes: 20 additions & 0 deletions src/linting/extended_checks.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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))

Expand Down Expand Up @@ -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
27 changes: 27 additions & 0 deletions test/rai_rules_tests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 2607c86

Please sign in to comment.