Merge pull request #390 from Manishearth/precedence
improved precedence messages (fixes #389)
This commit is contained in:
commit
a90a4540e9
@ -1,7 +1,9 @@
|
|||||||
use rustc::lint::*;
|
use rustc::lint::*;
|
||||||
use syntax::codemap::Spanned;
|
use syntax::codemap::Spanned;
|
||||||
use syntax::ast::*;
|
use syntax::ast::*;
|
||||||
use utils::span_lint;
|
use syntax::ast_util::binop_to_string;
|
||||||
|
|
||||||
|
use utils::{span_lint, snippet};
|
||||||
|
|
||||||
declare_lint!(pub PRECEDENCE, Warn,
|
declare_lint!(pub PRECEDENCE, Warn,
|
||||||
"catches operations where precedence may be unclear. See the wiki for a \
|
"catches operations where precedence may be unclear. See the wiki for a \
|
||||||
@ -19,10 +21,24 @@ fn get_lints(&self) -> LintArray {
|
|||||||
impl EarlyLintPass for Precedence {
|
impl EarlyLintPass for Precedence {
|
||||||
fn check_expr(&mut self, cx: &EarlyContext, expr: &Expr) {
|
fn check_expr(&mut self, cx: &EarlyContext, expr: &Expr) {
|
||||||
if let ExprBinary(Spanned { node: op, ..}, ref left, ref right) = expr.node {
|
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)) {
|
if !is_bit_op(op) { return; }
|
||||||
span_lint(cx, PRECEDENCE, expr.span,
|
match (is_arith_expr(left), is_arith_expr(right)) {
|
||||||
"operator precedence can trip the unwary. Consider adding parentheses \
|
(true, true) => span_lint(cx, PRECEDENCE, expr.span,
|
||||||
to the subexpression");
|
&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 @@ fn check_expr(&mut self, cx: &EarlyContext, expr: &Expr) {
|
|||||||
if let ExprLit(ref lit) = slf.node {
|
if let ExprLit(ref lit) = slf.node {
|
||||||
match lit.node {
|
match lit.node {
|
||||||
LitInt(..) | LitFloat(..) | LitFloatUnsuffixed(..) =>
|
LitInt(..) | LitFloat(..) | LitFloatUnsuffixed(..) =>
|
||||||
span_lint(cx, PRECEDENCE, expr.span,
|
span_lint(cx, PRECEDENCE, expr.span, &format!(
|
||||||
"unary minus has lower precedence than method call. Consider \
|
"unary minus has lower precedence than \
|
||||||
adding parentheses to clarify your intent"),
|
method call. Consider adding parentheses \
|
||||||
|
to clarify your intent: -({})",
|
||||||
|
snippet(cx, rhs.span, ".."))),
|
||||||
_ => ()
|
_ => ()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user