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

Nothing comparison with ==/!=: also check LHS for nothing #334

Merged
merged 1 commit into from
Apr 5, 2022
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
10 changes: 8 additions & 2 deletions src/linting/checks.jl
Original file line number Diff line number Diff line change
Expand Up @@ -363,9 +363,15 @@ end

function check_nothing_equality(x::EXPR, env::ExternalEnv)
if isbinarycall(x) && length(x.args) == 3
if valof(x.args[1]) == "==" && valof(x.args[3]) == "nothing" && refof(x.args[3]) === getsymbols(env)[:Core][:nothing]
if valof(x.args[1]) == "==" && (
(valof(x.args[2]) == "nothing" && refof(x.args[2]) === getsymbols(env)[:Core][:nothing]) ||
(valof(x.args[3]) == "nothing" && refof(x.args[3]) === getsymbols(env)[:Core][:nothing])
)
seterror!(x.args[1], NothingEquality)
elseif valof(x.args[1]) == "!=" && valof(x.args[3]) == "nothing" && refof(x.args[3]) === getsymbols(env)[:Core][:nothing]
elseif valof(x.args[1]) == "!=" && (
(valof(x.args[2]) == "nothing" && refof(x.args[2]) === getsymbols(env)[:Core][:nothing]) ||
(valof(x.args[3]) == "nothing" && refof(x.args[3]) === getsymbols(env)[:Core][:nothing])
)
seterror!(x.args[1], NothingNotEq)
end
end
Expand Down
5 changes: 4 additions & 1 deletion test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -300,9 +300,12 @@ f(arg) = arg
@test errorof(cst[4][2][3]) === nothing
end

let cst = parse_and_pass("a == nothing")
for cst in parse_and_pass.(["a == nothing", "nothing == a"])
@test errorof(cst[1][2]) === StaticLint.NothingEquality
end
for cst in parse_and_pass.(["a != nothing", "nothing != a"])
@test errorof(cst[1][2]) === StaticLint.NothingNotEq
end

let cst = parse_and_pass("""
struct Graph
Expand Down