summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--perl_checker.src/parser_helper.ml10
-rw-r--r--perl_checker.src/test/suggest_better.t4
2 files changed, 14 insertions, 0 deletions
diff --git a/perl_checker.src/parser_helper.ml b/perl_checker.src/parser_helper.ml
index 8a81bda..727f956 100644
--- a/perl_checker.src/parser_helper.ml
+++ b/perl_checker.src/parser_helper.ml
@@ -439,12 +439,22 @@ let check_scalar_subscripted esp =
| Deref(I_scalar, Deref _) -> warn_rule "for complex dereferencing, use \"->\""
| _ -> ()
+let negatable_ops = collect (fun (a, b) -> [ a, b ; b, a ]) [
+ "==", "!=" ;
+ "eq", "ne" ;
+]
+
let check_negatable_expr esp =
match un_parenthesize_full esp.any.expr with
| Call_op("m//", var :: _, _) when not (is_var_dollar_ var) ->
warn_rule "!($var =~ /.../) is better written $var !~ /.../"
| Call_op("!m//", var :: _, _) when not (is_var_dollar_ var) ->
warn_rule "!($var !~ /.../) is better written $var =~ /.../"
+ | Call_op(op, _, _) ->
+ (try
+ let neg_op = List.assoc op negatable_ops in
+ warn_rule (Printf.sprintf "!($foo %s $bar) is better written $foo %s $bar" op neg_op)
+ with Not_found -> ())
| _ -> ()
let check_ternary_paras(cond, a, b) =
diff --git a/perl_checker.src/test/suggest_better.t b/perl_checker.src/test/suggest_better.t
index 58a1308..caa3677 100644
--- a/perl_checker.src/test/suggest_better.t
+++ b/perl_checker.src/test/suggest_better.t
@@ -62,6 +62,10 @@ member($xxx, keys %h) you can replace "member($xxx, keys %yyy
!($xxx =~ /.../) !($var =~ /.../) is better written $var !~ /.../
+!($xxx == 1) !($foo == $bar) is better written $foo != $bar
+
+!($xxx eq 'foo') !($foo eq $bar) is better written $foo ne $bar
+
foreach (@l) { use "push @l2, grep { ... } ..." instead of "foreach (...) { push @l2, $_ if ... }"
push @l2, $_ if yyy($_); or sometimes "@l2 = grep { ... } ..."
}