Skip to content

Commit 84e7a7a

Browse files
committed
Merge branch 'master' into workspacepackags
2 parents 100a49b + e7b0f58 commit 84e7a7a

File tree

2 files changed

+34
-4
lines changed

2 files changed

+34
-4
lines changed

src/linting/checks.jl

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,8 @@ MissingFile,
1616
InvalidModuleName,
1717
TypePiracy,
1818
CannotDeclareConst,
19-
InvalidRedefofConst)
19+
InvalidRedefofConst,
20+
NotEqDef)
2021

2122
const LintCodeDescriptions = Dict{LintCodes,String}(IncorrectCallArgs => "Possible method call error.",
2223
IncorrectIterSpec => "A loop iterator has been used that will likely error.",
@@ -34,7 +35,8 @@ const LintCodeDescriptions = Dict{LintCodes,String}(IncorrectCallArgs => "Possib
3435
InvalidModuleName => "Module name matches that of its parent.",
3536
TypePiracy => "An imported function has been extended without using module defined typed arguments.",
3637
CannotDeclareConst => "Cannot declare constant; it already has a value.",
37-
InvalidRedefofConst => "Invalid redefinition of constant.")
38+
InvalidRedefofConst => "Invalid redefinition of constant.",
39+
NotEqDef => "`!=` is defined as `const != = !(==)` and should not be overloaded. Overload `==` instead.")
3840

3941
haserror(m::Meta) = m.error !== nothing
4042
haserror(x::EXPR) = hasmeta(x) && haserror(x.meta)
@@ -489,9 +491,11 @@ function check_typeparams(x::EXPR)
489491
end
490492

491493
function check_for_pirates(x::EXPR)
492-
if CSTParser.defines_function(x) && hasbinding(x) && overwrites_imported_function(bindingof(x))
494+
if CSTParser.defines_function(x)
493495
sig = CSTParser.rem_where_decl(CSTParser.get_sig(x))
494-
if typof(sig) == CSTParser.Call
496+
if fname_is_noteq(CSTParser.get_name(sig))
497+
seterror!(x, NotEqDef)
498+
elseif typof(sig) == CSTParser.Call && hasbinding(x) && overwrites_imported_function(bindingof(x))
495499
for i = 2:length(sig)
496500
if hasbinding(sig[i]) && bindingof(sig[i]).type isa Binding
497501
return
@@ -504,6 +508,18 @@ function check_for_pirates(x::EXPR)
504508
end
505509
end
506510

511+
function fname_is_noteq(x)
512+
if x isa EXPR
513+
if typof(x) === CSTParser.OPERATOR && kindof(x) === CSTParser.Tokens.NOT_EQ
514+
return true
515+
elseif is_getfield_w_quotenode(x) && length(x[3]) == 2 && CSTParser.is_colon(x[3][1])
516+
517+
return fname_is_noteq(x[3][2])
518+
end
519+
end
520+
return false
521+
end
522+
507523
function refers_to_nonimported_type(arg::EXPR)
508524
if hasref(arg) && refof(arg) isa Binding
509525
return true

test/runtests.jl

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -481,6 +481,20 @@ end
481481
@test errorof(cst[5]) === StaticLint.TypePiracy
482482
@test errorof(cst[6]) === StaticLint.TypePiracy
483483
end
484+
let cst = parse_and_pass("""
485+
!=(a,b) = true
486+
Base.:!=(a,b) = true
487+
!=(a::T,b::T) = true
488+
!=(a::T,b::T) where T= true
489+
""")
490+
StaticLint.check_for_pirates.(cst)
491+
492+
493+
@test errorof(cst[1]) === StaticLint.NotEqDef
494+
@test errorof(cst[2]) === StaticLint.NotEqDef
495+
@test errorof(cst[3]) === StaticLint.NotEqDef
496+
@test errorof(cst[4]) === StaticLint.NotEqDef
497+
end
484498
end
485499

486500
@testset "docs for undescribed variables" begin

0 commit comments

Comments
 (0)