summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--perl_checker.src/parser_helper.ml16
-rw-r--r--perl_checker.src/test/various_errors.t4
2 files changed, 20 insertions, 0 deletions
diff --git a/perl_checker.src/parser_helper.ml b/perl_checker.src/parser_helper.ml
index d797daf..cd5a7f1 100644
--- a/perl_checker.src/parser_helper.ml
+++ b/perl_checker.src/parser_helper.ml
@@ -658,6 +658,13 @@ let remove_call_with_same_para_special = function
| Call(f, [Deref(I_star, (Ident(None, "_", _)))]) -> f
| e -> e
+let check_My_under_condition msg = function
+ | List [ My_our("my", _, _) ] ->
+ warn_rule "this is stupid"
+ | List [ Call_op("=", [ My_our("my", _, _); _ ], _) ] ->
+ warn_rule msg
+ | _ -> ()
+
let cook_call_op op para pos =
(match op with
| "le" | "ge" | "eq" | "ne" | "gt" | "lt" | "cmp" ->
@@ -720,6 +727,9 @@ let cook_call_op op para pos =
| "or", [ List [ Deref(I_scalar, id) ]; List [ Call_op("=", [ Deref(I_scalar, id_); _], _) ] ] when is_same_fromparser id id_ ->
warn_rule "\"$foo or $foo = ...\" can be written \"$foo ||= ...\""
+
+ | "and", [ cond ; expr ] -> check_My_under_condition "replace \"<cond> and my $foo = ...\" with \"my $foo = <cond> && ...\"" expr
+ | "or", [ cond ; expr ] -> check_My_under_condition "replace \"<cond> or my $foo = ...\" with \"my $foo = !<cond> && ...\"" expr
| _ -> ());
@@ -1266,6 +1276,10 @@ let call_op_if_infix left right esp_start esp_end =
| _ -> ());
mcontext_check_none "value is dropped" [left] esp_start;
+ (match right with
+ | List [ Num("0", _)] -> () (* allow my $x if 0 *)
+ | _ -> check_My_under_condition "replace \"my $foo = ... if <cond>\" with \"my $foo = <cond> && ...\"" left);
+
let pos = raw_pos_range esp_start esp_end in
new_any M_none (Call_op("if infix", [ left ; right], raw_pos2pos pos)) esp_start.spaces pos
@@ -1283,6 +1297,8 @@ let call_op_unless_infix left right esp_start esp_end =
| _ -> ());
mcontext_check_none "value is dropped" [left] esp_start;
+ check_My_under_condition "replace \"my $foo = ... unless <cond>\" with \"my $foo = !<cond> && ...\"" left;
+
let pos = raw_pos_range esp_start esp_end in
new_any M_none (Call_op("unless infix", [ left ; right], raw_pos2pos pos)) esp_start.spaces pos
diff --git a/perl_checker.src/test/various_errors.t b/perl_checker.src/test/various_errors.t
index 901612f..dabb641 100644
--- a/perl_checker.src/test/various_errors.t
+++ b/perl_checker.src/test/various_errors.t
@@ -6,6 +6,10 @@ $xxx[1, 2] you must give only one argument
$xxx[] you must give one argument
+my $_x = 'xxx' if $xxx; replace "my $foo = ... if <cond>" with "my $foo = <cond> && ..."
+
+$xxx or my $_x = 'xxx'; replace "<cond> or my $foo = ..." with "my $foo = !<cond> && ..."
+
'' || 'xxx' <constant> || ... is the same as ...
if ($xxx = '') {} are you sure you did not mean "==" instead of "="?