summaryrefslogtreecommitdiffstats
path: root/perl_checker.src
diff options
context:
space:
mode:
authorPascal Rigaux <pixel@mandriva.com>2002-12-04 13:16:15 +0000
committerPascal Rigaux <pixel@mandriva.com>2002-12-04 13:16:15 +0000
commitaafa35726b096f3e3b4967c0692a9abaee130ba1 (patch)
tree18981f3fe185ff36c8db663adf6bd49961b50b1c /perl_checker.src
parent701155c3ec847cb0cb7ee5ed5a4d56945fe21ab3 (diff)
downloadperl_checker-aafa35726b096f3e3b4967c0692a9abaee130ba1.tar
perl_checker-aafa35726b096f3e3b4967c0692a9abaee130ba1.tar.gz
perl_checker-aafa35726b096f3e3b4967c0692a9abaee130ba1.tar.bz2
perl_checker-aafa35726b096f3e3b4967c0692a9abaee130ba1.tar.xz
perl_checker-aafa35726b096f3e3b4967c0692a9abaee130ba1.zip
- warn use of "cond ? list : ()" (use if_(cond, list) instead)
- adapt MDK::Common::* to this (using @{[]} instead of () to avoid the warning)
Diffstat (limited to 'perl_checker.src')
-rw-r--r--perl_checker.src/parser.mly10
-rw-r--r--perl_checker.src/parser_helper.ml14
-rw-r--r--perl_checker.src/parser_helper.mli10
3 files changed, 27 insertions, 7 deletions
diff --git a/perl_checker.src/parser.mly b/perl_checker.src/parser.mly
index 695e88a..45c0db8 100644
--- a/perl_checker.src/parser.mly
+++ b/perl_checker.src/parser.mly
@@ -233,10 +233,10 @@ term:
| term PATTERN_MATCH_NOT STRING {die_with_rawpos (sndsnd $3) "use a regexp, not a string"}
-| term QUESTION_MARK term COLON term {sp_p($2); sp_p($3); sp_p($4); sp_p($5); to_Call_op_(P_ternary, "?:", [ prio_lo P_ternary $1 ; prio_lo_after P_ternary $3; prio_lo_after P_ternary $5]) (sp_pos_range $1 $5)}
-| term QUESTION_MARK term COLON BRACKET expr BRACKET_END {sp_p($2); sp_p($3); sp_p($4); sp_p($5); sp_p($6); sp_p($7); to_Call_op_(P_ternary, "?:", [ prio_lo P_ternary $1 ; prio_lo_after P_ternary $3; sndfst $6]) (sp_pos_range $1 $7)}
-| term QUESTION_MARK BRACKET expr BRACKET_END COLON term {sp_p($2); sp_p($3); sp_p($4); sp_p($5); sp_p($6); sp_p($7); to_Call_op_(P_ternary, "?:", [ prio_lo P_ternary $1 ; sndfst $4; prio_lo_after P_ternary $7]) (sp_pos_range $1 $7)}
-| term QUESTION_MARK BRACKET expr BRACKET_END COLON BRACKET expr BRACKET_END {sp_p($2); sp_p($3); sp_p($4); sp_p($5); sp_p($6); sp_p($7); sp_p($8); sp_p($9); to_Call_op_(P_ternary, "?:", [ prio_lo P_ternary $1 ; sndfst $4; sndfst $8]) (sp_pos_range $1 $9)}
+| term QUESTION_MARK term COLON term {sp_p($2); sp_p($3); sp_p($4); sp_p($5); check_ternary_paras $3 $5; to_Call_op_(P_ternary, "?:", [ prio_lo P_ternary $1 ; prio_lo_after P_ternary $3; prio_lo_after P_ternary $5]) (sp_pos_range $1 $5)}
+| term QUESTION_MARK term COLON BRACKET expr BRACKET_END {sp_p($2); sp_p($3); check_ternary_para $3; sp_p($4); sp_p($5); sp_p($6); sp_p($7); check_ternary_para $1; to_Call_op_(P_ternary, "?:", [ prio_lo P_ternary $1 ; prio_lo_after P_ternary $3; Ref(I_hash, sndfst $6)]) (sp_pos_range $1 $7)}
+| term QUESTION_MARK BRACKET expr BRACKET_END COLON term {sp_p($2); sp_p($3); sp_p($4); sp_p($5); sp_p($6); sp_p($7); check_ternary_para $7; to_Call_op_(P_ternary, "?:", [ prio_lo P_ternary $1 ; Ref(I_hash, sndfst $4); prio_lo_after P_ternary $7]) (sp_pos_range $1 $7)}
+| term QUESTION_MARK BRACKET expr BRACKET_END COLON BRACKET expr BRACKET_END {sp_p($2); sp_p($3); sp_p($4); sp_p($5); sp_p($6); sp_p($7); sp_p($8); sp_p($9); to_Call_op_(P_ternary, "?:", [ prio_lo P_ternary $1 ; Ref(I_hash, sndfst $4); Ref(I_hash, sndfst $8)]) (sp_pos_range $1 $9)}
/* Unary operators and terms */
@@ -296,6 +296,8 @@ term:
| term ARROW word_or_scalar {sp_0($2); sp_0($3); (P_tok, to_Method_call(sndfst $1, fst $3, [])), sp_pos_range $1 $3} /* $foo->bar */
| term ARROW MULT parenthesized {check_MULT_is_x $3; sp_0($2); sp_0($3); sp_0($4); (P_tok, to_Method_call(sndfst $1, Ident(None, "x", get_pos $3), sndfst $4)), sp_pos_range $1 $4} /* $foo->bar(list) */
| term ARROW MULT {check_MULT_is_x $3; sp_0($2); sp_0($3); (P_tok, to_Method_call(sndfst $1, Ident(None, "x", get_pos $3), [])), sp_pos_range $1 $3} /* $foo->bar */
+| term ARROW FOR parenthesized {sp_0($2); sp_0($3); sp_0($4); (P_tok, to_Method_call(sndfst $1, Ident(None, fst $3, get_pos $3), sndfst $4)), sp_pos_range $1 $4} /* $foo->bar(list) */
+| term ARROW FOR {sp_0($2); sp_0($3); (P_tok, to_Method_call(sndfst $1, Ident(None, fst $3, get_pos $3), [])), sp_pos_range $1 $3} /* $foo->bar */
| NEW word { sp_n($2); (P_call_no_paren, to_Method_call(fst $2, Ident(None, "new", get_pos $1), [])), sp_pos_range $1 $2} /* new Class */
| NEW word_paren parenthesized { sp_n($2); sp_0($3); (P_call_no_paren, to_Method_call(fst $2, Ident(None, "new", get_pos $1), sndfst $3)), sp_pos_range $1 $3} /* new Class(...) */
diff --git a/perl_checker.src/parser_helper.ml b/perl_checker.src/parser_helper.ml
index 1e03f0e..00b121d 100644
--- a/perl_checker.src/parser_helper.ml
+++ b/perl_checker.src/parser_helper.ml
@@ -15,7 +15,10 @@ let is_var_dollar_ = function
| Deref(I_scalar, Ident(None, "_", _)) -> true
| _ -> false
let is_var_number_match = function
- | Deref(I_scalar, Ident(None, s, _)) -> String.length s = 1 && char_is_number s.[0]
+ | Deref(I_scalar, Ident(None, s, _)) -> String.length s = 1 && s.[0] <> '0' && char_is_number s.[0]
+ | _ -> false
+let is_call = function
+ | Call _ -> true
| _ -> false
let is_parenthesized = function
@@ -279,6 +282,15 @@ let check_arrow_needed ((_, e), _) ter =
| Deref_with _ -> warn (sndsnd ter) "the arrow \"->\" is unneeded"
| _ -> ()
+let check_ternary_para ((_, e), _) =
+ match e with
+ | List [] -> warn_rule "you may use if_() here\n beware that the short-circuit semantic of ?: is not kept\n if you want to keep the short-circuit behaviour, replace () with @{[]} and there will be no warning anymore"
+ | _ -> ()
+
+let check_ternary_paras ((_, e1), _ as ter1) ((_, e2), _ as ter2) =
+ if not (is_call e1) then check_ternary_para ter2;
+ if not (is_call e2) then check_ternary_para ter1
+
let check_unneeded_var_dollar_ ((_, e), (_, pos)) =
if is_var_dollar_ e then warn pos "\"$_ =~ /regexp/\" can be written \"/regexp/\"" else
if is_var_number_match e then warn pos "do not use the result of a match (eg: $1) to match another pattern"
diff --git a/perl_checker.src/parser_helper.mli b/perl_checker.src/parser_helper.mli
index e617547..d3f5396 100644
--- a/perl_checker.src/parser_helper.mli
+++ b/perl_checker.src/parser_helper.mli
@@ -8,6 +8,8 @@ val get_pos : 'a * ('b * ('c * 'd)) -> string * 'c * 'd
val var_dollar_ : Types.fromparser
val var_STDOUT : Types.fromparser
val is_var_dollar_ : Types.fromparser -> bool
+val is_var_number_match : Types.fromparser -> bool
+val is_call : Types.fromparser -> bool
val is_parenthesized : Types.fromparser -> bool
val un_parenthesize : Types.fromparser -> Types.fromparser
val un_parenthesize_full : Types.fromparser -> Types.fromparser
@@ -60,12 +62,16 @@ val check_hash_subscript :
('a * Types.fromparser) * ('b * (int * int)) -> unit
val check_arrow_needed :
('a * Types.fromparser) * 'b -> 'c * ('d * (int * int)) -> unit
+val check_ternary_para : ('a * Types.fromparser) * ('b * (int * int)) -> unit
+val check_ternary_paras :
+ ('a * Types.fromparser) * ('b * (int * int)) ->
+ ('c * Types.fromparser) * ('d * (int * int)) -> unit
val check_unneeded_var_dollar_ :
('a * Types.fromparser) * ('b * (int * int)) -> unit
-val check_unneeded_var_dollar_s :
- ('a * Types.fromparser) * ('b * (int * int)) -> unit
val check_unneeded_var_dollar_not :
('a * Types.fromparser) * ('b * (int * int)) -> unit
+val check_unneeded_var_dollar_s :
+ ('a * Types.fromparser) * ('b * (int * int)) -> unit
val check_MULT_is_x : string * 'a -> unit
val check_my : string * 'a -> unit
val check_foreach : string * ('a * (int * int)) -> unit