Merge pull request #390 from Manishearth/precedence

improved precedence messages (fixes #389)
This commit is contained in:
Manish Goregaokar 2015-10-13 17:33:23 +05:30
commit a90a4540e9
1 changed files with 26 additions and 8 deletions

View File

@ -1,7 +1,9 @@
use rustc::lint::*;
use syntax::codemap::Spanned;
use syntax::ast::*;
use utils::span_lint;
use syntax::ast_util::binop_to_string;
use utils::{span_lint, snippet};
declare_lint!(pub PRECEDENCE, Warn,
"catches operations where precedence may be unclear. See the wiki for a \
@ -19,10 +21,24 @@ impl LintPass for Precedence {
impl EarlyLintPass for Precedence {
fn check_expr(&mut self, cx: &EarlyContext, expr: &Expr) {
if let ExprBinary(Spanned { node: op, ..}, ref left, ref right) = expr.node {
if is_bit_op(op) && (is_arith_expr(left) || is_arith_expr(right)) {
span_lint(cx, PRECEDENCE, expr.span,
"operator precedence can trip the unwary. Consider adding parentheses \
to the subexpression");
if !is_bit_op(op) { return; }
match (is_arith_expr(left), is_arith_expr(right)) {
(true, true) => span_lint(cx, PRECEDENCE, expr.span,
&format!("operator precedence can trip the unwary. \
Consider parenthesizing your expression:\
`({}) {} ({})`", snippet(cx, left.span, ".."),
binop_to_string(op), snippet(cx, right.span, ".."))),
(true, false) => span_lint(cx, PRECEDENCE, expr.span,
&format!("operator precedence can trip the unwary. \
Consider parenthesizing your expression:\
`({}) {} {}`", snippet(cx, left.span, ".."),
binop_to_string(op), snippet(cx, right.span, ".."))),
(false, true) => span_lint(cx, PRECEDENCE, expr.span,
&format!("operator precedence can trip the unwary. \
Consider parenthesizing your expression:\
`{} {} ({})`", snippet(cx, left.span, ".."),
binop_to_string(op), snippet(cx, right.span, ".."))),
_ => (),
}
}
@ -32,9 +48,11 @@ impl EarlyLintPass for Precedence {
if let ExprLit(ref lit) = slf.node {
match lit.node {
LitInt(..) | LitFloat(..) | LitFloatUnsuffixed(..) =>
span_lint(cx, PRECEDENCE, expr.span,
"unary minus has lower precedence than method call. Consider \
adding parentheses to clarify your intent"),
span_lint(cx, PRECEDENCE, expr.span, &format!(
"unary minus has lower precedence than \
method call. Consider adding parentheses \
to clarify your intent: -({})",
snippet(cx, rhs.span, ".."))),
_ => ()
}
}