Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding in, hashkey, equal #29

Merged
merged 1 commit into from
Mar 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading