Skip to content

Commit 7b45261

Browse files
authored
Merge pull request #334 from fredrikekre/fe/nothing
Nothing comparison with ==/!=: also check LHS for nothing
2 parents 06d008e + d4e4e18 commit 7b45261

File tree

2 files changed

+12
-3
lines changed

2 files changed

+12
-3
lines changed

src/linting/checks.jl

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -363,9 +363,15 @@ end
363363

364364
function check_nothing_equality(x::EXPR, env::ExternalEnv)
365365
if isbinarycall(x) && length(x.args) == 3
366-
if valof(x.args[1]) == "==" && valof(x.args[3]) == "nothing" && refof(x.args[3]) === getsymbols(env)[:Core][:nothing]
366+
if valof(x.args[1]) == "==" && (
367+
(valof(x.args[2]) == "nothing" && refof(x.args[2]) === getsymbols(env)[:Core][:nothing]) ||
368+
(valof(x.args[3]) == "nothing" && refof(x.args[3]) === getsymbols(env)[:Core][:nothing])
369+
)
367370
seterror!(x.args[1], NothingEquality)
368-
elseif valof(x.args[1]) == "!=" && valof(x.args[3]) == "nothing" && refof(x.args[3]) === getsymbols(env)[:Core][:nothing]
371+
elseif valof(x.args[1]) == "!=" && (
372+
(valof(x.args[2]) == "nothing" && refof(x.args[2]) === getsymbols(env)[:Core][:nothing]) ||
373+
(valof(x.args[3]) == "nothing" && refof(x.args[3]) === getsymbols(env)[:Core][:nothing])
374+
)
369375
seterror!(x.args[1], NothingNotEq)
370376
end
371377
end

test/runtests.jl

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -300,9 +300,12 @@ f(arg) = arg
300300
@test errorof(cst[4][2][3]) === nothing
301301
end
302302

303-
let cst = parse_and_pass("a == nothing")
303+
for cst in parse_and_pass.(["a == nothing", "nothing == a"])
304304
@test errorof(cst[1][2]) === StaticLint.NothingEquality
305305
end
306+
for cst in parse_and_pass.(["a != nothing", "nothing != a"])
307+
@test errorof(cst[1][2]) === StaticLint.NothingNotEq
308+
end
306309

307310
let cst = parse_and_pass("""
308311
struct Graph

0 commit comments

Comments
 (0)