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 \
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, ".."))),
+ _ => (),
}
}
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, ".."))),
_ => ()
}
}