1 use crate::utils::{differing_macro_contexts, snippet_opt, span_lint_and_help, span_lint_and_note};
2 use if_chain::if_chain;
3 use rustc_ast::ast::{BinOpKind, Block, Expr, ExprKind, StmtKind, UnOp};
4 use rustc_lint::{EarlyContext, EarlyLintPass};
5 use rustc_middle::lint::in_external_macro;
6 use rustc_session::{declare_lint_pass, declare_tool_lint};
7 use rustc_span::source_map::Span;
10 /// **What it does:** Checks for use of the non-existent `=*`, `=!` and `=-`
13 /// **Why is this bad?** This is either a typo of `*=`, `!=` or `-=` or
16 /// **Known problems:** None.
20 /// a =- 42; // confusing, should it be `a -= 42` or `a = -42`?
22 pub SUSPICIOUS_ASSIGNMENT_FORMATTING,
24 "suspicious formatting of `*=`, `-=` or `!=`"
27 declare_clippy_lint! {
28 /// **What it does:** Checks the formatting of a unary operator on the right hand side
29 /// of a binary operator. It lints if there is no space between the binary and unary operators,
30 /// but there is a space between the unary and its operand.
32 /// **Why is this bad?** This is either a typo in the binary operator or confusing.
34 /// **Known problems:** None.
38 /// if foo <- 30 { // this should be `foo < -30` but looks like a different operator
41 /// if foo &&! bar { // this should be `foo && !bar` but looks like a different operator
44 pub SUSPICIOUS_UNARY_OP_FORMATTING,
46 "suspicious formatting of unary `-` or `!` on the RHS of a BinOp"
49 declare_clippy_lint! {
50 /// **What it does:** Checks for formatting of `else`. It lints if the `else`
51 /// is followed immediately by a newline or the `else` seems to be missing.
53 /// **Why is this bad?** This is probably some refactoring remnant, even if the
54 /// code is correct, it might look confusing.
56 /// **Known problems:** None.
61 /// } { // looks like an `else` is missing here
65 /// } if bar { // looks like an `else` is missing here
71 /// { // this is the `else` block of the previous `if`, but should it be?
77 /// if bar { // this is the `else` block of the previous `if`, but should it be?
80 pub SUSPICIOUS_ELSE_FORMATTING,
82 "suspicious formatting of `else`"
85 declare_clippy_lint! {
86 /// **What it does:** Checks for possible missing comma in an array. It lints if
87 /// an array element is a binary operator expression and it lies on two lines.
89 /// **Why is this bad?** This could lead to unexpected results.
91 /// **Known problems:** None.
96 /// -1, -2, -3 // <= no comma here
100 pub POSSIBLE_MISSING_COMMA,
102 "possible missing comma in array"
105 declare_lint_pass!(Formatting => [
106 SUSPICIOUS_ASSIGNMENT_FORMATTING,
107 SUSPICIOUS_UNARY_OP_FORMATTING,
108 SUSPICIOUS_ELSE_FORMATTING,
109 POSSIBLE_MISSING_COMMA
112 impl EarlyLintPass for Formatting {
113 fn check_block(&mut self, cx: &EarlyContext<'_>, block: &Block) {
114 for w in block.stmts.windows(2) {
115 match (&w[0].kind, &w[1].kind) {
116 (&StmtKind::Expr(ref first), &StmtKind::Expr(ref second))
117 | (&StmtKind::Expr(ref first), &StmtKind::Semi(ref second)) => {
118 check_missing_else(cx, first, second);
125 fn check_expr(&mut self, cx: &EarlyContext<'_>, expr: &Expr) {
126 check_assign(cx, expr);
127 check_unop(cx, expr);
128 check_else(cx, expr);
129 check_array(cx, expr);
133 /// Implementation of the `SUSPICIOUS_ASSIGNMENT_FORMATTING` lint.
134 fn check_assign(cx: &EarlyContext<'_>, expr: &Expr) {
135 if let ExprKind::Assign(ref lhs, ref rhs, _) = expr.kind {
136 if !differing_macro_contexts(lhs.span, rhs.span) && !lhs.span.from_expansion() {
137 let eq_span = lhs.span.between(rhs.span);
138 if let ExprKind::Unary(op, ref sub_rhs) = rhs.kind {
139 if let Some(eq_snippet) = snippet_opt(cx, eq_span) {
140 let op = UnOp::to_string(op);
141 let eqop_span = lhs.span.between(sub_rhs.span);
142 if eq_snippet.ends_with('=') {
145 SUSPICIOUS_ASSIGNMENT_FORMATTING,
148 "this looks like you are trying to use `.. {op}= ..`, but you \
149 really are doing `.. = ({op} ..)`",
153 &format!("to remove this lint, use either `{op}=` or `= {op}`", op = op),
162 /// Implementation of the `SUSPICIOUS_UNARY_OP_FORMATTING` lint.
163 fn check_unop(cx: &EarlyContext<'_>, expr: &Expr) {
165 if let ExprKind::Binary(ref binop, ref lhs, ref rhs) = expr.kind;
166 if !differing_macro_contexts(lhs.span, rhs.span) && !lhs.span.from_expansion();
167 // span between BinOp LHS and RHS
168 let binop_span = lhs.span.between(rhs.span);
170 if let ExprKind::Unary(op, ref un_rhs) = rhs.kind;
171 // from UnOp operator to UnOp operand
172 let unop_operand_span = rhs.span.until(un_rhs.span);
173 if let Some(binop_snippet) = snippet_opt(cx, binop_span);
174 if let Some(unop_operand_snippet) = snippet_opt(cx, unop_operand_span);
175 let binop_str = BinOpKind::to_string(&binop.node);
176 // no space after BinOp operator and space after UnOp operator
177 if binop_snippet.ends_with(binop_str) && unop_operand_snippet.ends_with(' ');
179 let unop_str = UnOp::to_string(op);
180 let eqop_span = lhs.span.between(un_rhs.span);
183 SUSPICIOUS_UNARY_OP_FORMATTING,
186 "by not having a space between `{binop}` and `{unop}` it looks like \
187 `{binop}{unop}` is a single operator",
193 "put a space between `{binop}` and `{unop}` and remove the space after `{unop}`",
202 /// Implementation of the `SUSPICIOUS_ELSE_FORMATTING` lint for weird `else`.
203 fn check_else(cx: &EarlyContext<'_>, expr: &Expr) {
205 if let ExprKind::If(_, then, Some(else_)) = &expr.kind;
206 if is_block(else_) || is_if(else_);
207 if !differing_macro_contexts(then.span, else_.span);
208 if !then.span.from_expansion() && !in_external_macro(cx.sess, expr.span);
210 // workaround for rust-lang/rust#43081
211 if expr.span.lo().0 != 0 && expr.span.hi().0 != 0;
213 // this will be a span from the closing ‘}’ of the “then” block (excluding) to
214 // the “if” of the “else if” block (excluding)
215 let else_span = then.span.between(else_.span);
217 // the snippet should look like " else \n " with maybe comments anywhere
218 // it’s bad when there is a ‘\n’ after the “else”
219 if let Some(else_snippet) = snippet_opt(cx, else_span);
220 if let Some(else_pos) = else_snippet.find("else");
221 if else_snippet[else_pos..].contains('\n');
222 let else_desc = if is_if(else_) { "if" } else { "{..}" };
227 SUSPICIOUS_ELSE_FORMATTING,
229 &format!("this is an `else {}` but the formatting might hide it", else_desc),
232 "to remove this lint, remove the `else` or remove the new line between \
242 fn has_unary_equivalent(bin_op: BinOpKind) -> bool {
244 bin_op == BinOpKind::And || bin_op == BinOpKind::Mul || bin_op == BinOpKind::Sub
247 fn indentation(cx: &EarlyContext<'_>, span: Span) -> usize {
248 cx.sess.source_map().lookup_char_pos(span.lo()).col.0
251 /// Implementation of the `POSSIBLE_MISSING_COMMA` lint for array
252 fn check_array(cx: &EarlyContext<'_>, expr: &Expr) {
253 if let ExprKind::Array(ref array) = expr.kind {
254 for element in array {
256 if let ExprKind::Binary(ref op, ref lhs, _) = element.kind;
257 if has_unary_equivalent(op.node) && !differing_macro_contexts(lhs.span, op.span);
258 let space_span = lhs.span.between(op.span);
259 if let Some(space_snippet) = snippet_opt(cx, space_span);
260 let lint_span = lhs.span.with_lo(lhs.span.hi());
261 if space_snippet.contains('\n');
262 if indentation(cx, op.span) <= indentation(cx, lhs.span);
266 POSSIBLE_MISSING_COMMA,
268 "possibly missing a comma here",
270 "to remove this lint, add a comma or write the expr in a single line",
278 fn check_missing_else(cx: &EarlyContext<'_>, first: &Expr, second: &Expr) {
279 if !differing_macro_contexts(first.span, second.span)
280 && !first.span.from_expansion()
282 && (is_block(second) || is_if(second))
284 // where the else would be
285 let else_span = first.span.between(second.span);
287 if let Some(else_snippet) = snippet_opt(cx, else_span) {
288 if !else_snippet.contains('\n') {
289 let (looks_like, next_thing) = if is_if(second) {
290 ("an `else if`", "the second `if`")
292 ("an `else {..}`", "the next block")
297 SUSPICIOUS_ELSE_FORMATTING,
299 &format!("this looks like {} but the `else` is missing", looks_like),
302 "to remove this lint, add the missing `else` or add a new line before {}",
311 fn is_block(expr: &Expr) -> bool {
312 if let ExprKind::Block(..) = expr.kind {
319 /// Check if the expression is an `if` or `if let`
320 fn is_if(expr: &Expr) -> bool {
321 if let ExprKind::If(..) = expr.kind {