1 use clippy_utils::diagnostics::{span_lint_and_note, span_lint_and_then};
2 use clippy_utils::source::{first_line_of_span, indent_of, reindent_multiline, snippet, snippet_opt};
4 both, count_eq, eq_expr_value, get_enclosing_block, get_parent_expr, if_sequence, in_macro, parent_node_is_if_expr,
5 run_lints, search_same, ContainsName, SpanlessEq, SpanlessHash,
7 use if_chain::if_chain;
8 use rustc_data_structures::fx::FxHashSet;
9 use rustc_errors::{Applicability, DiagnosticBuilder};
10 use rustc_hir::intravisit::{self, NestedVisitorMap, Visitor};
11 use rustc_hir::{Block, Expr, ExprKind, HirId};
12 use rustc_lint::{LateContext, LateLintPass};
13 use rustc_middle::hir::map::Map;
14 use rustc_session::{declare_lint_pass, declare_tool_lint};
15 use rustc_span::{source_map::Span, symbol::Symbol, BytePos};
18 declare_clippy_lint! {
19 /// **What it does:** Checks for consecutive `if`s with the same condition.
21 /// **Why is this bad?** This is probably a copy & paste error.
23 /// **Known problems:** Hopefully none.
29 /// } else if a == b {
34 /// Note that this lint ignores all conditions with a function call as it could
35 /// have side effects:
40 /// } else if foo() { // not linted
46 "consecutive `if`s with the same condition"
49 declare_clippy_lint! {
50 /// **What it does:** Checks for consecutive `if`s with the same function call.
52 /// **Why is this bad?** This is probably a copy & paste error.
53 /// Despite the fact that function can have side effects and `if` works as
54 /// intended, such an approach is implicit and can be considered a "code smell".
56 /// **Known problems:** Hopefully none.
62 /// } else if foo() == bar {
67 /// This probably should be:
71 /// } else if foo() == baz {
76 /// or if the original code was not a typo and called function mutates a state,
77 /// consider move the mutation out of the `if` condition to avoid similarity to
78 /// a copy & paste error:
81 /// let first = foo();
85 /// let second = foo();
86 /// if second == bar {
91 pub SAME_FUNCTIONS_IN_IF_CONDITION,
93 "consecutive `if`s with the same function call"
96 declare_clippy_lint! {
97 /// **What it does:** Checks for `if/else` with the same body as the *then* part
98 /// and the *else* part.
100 /// **Why is this bad?** This is probably a copy & paste error.
102 /// **Known problems:** Hopefully none.
112 pub IF_SAME_THEN_ELSE,
114 "`if` with the same `then` and `else` blocks"
117 declare_clippy_lint! {
118 /// **What it does:** Checks if the `if` and `else` block contain shared code that can be
119 /// moved out of the blocks.
121 /// **Why is this bad?** Duplicate code is less maintainable.
123 /// **Known problems:** Hopefully none.
128 /// println!("Hello World");
131 /// println!("Hello World");
136 /// Could be written as:
138 /// println!("Hello World");
145 pub BRANCHES_SHARING_CODE,
147 "`if` statement with shared code in all blocks"
150 declare_lint_pass!(CopyAndPaste => [
152 SAME_FUNCTIONS_IN_IF_CONDITION,
154 BRANCHES_SHARING_CODE
157 impl<'tcx> LateLintPass<'tcx> for CopyAndPaste {
158 fn check_expr(&mut self, cx: &LateContext<'tcx>, expr: &'tcx Expr<'_>) {
159 if !expr.span.from_expansion() {
160 if let ExprKind::If(_, _, _) = expr.kind {
161 // skip ifs directly in else, it will be checked in the parent if
163 kind: ExprKind::If(_, _, Some(else_expr)),
165 }) = get_parent_expr(cx, expr)
167 if else_expr.hir_id == expr.hir_id {
172 let (conds, blocks) = if_sequence(expr);
174 lint_same_cond(cx, &conds);
175 lint_same_fns_in_if_cond(cx, &conds);
177 lint_same_then_else(cx, &blocks, conds.len() == blocks.len(), expr);
183 /// Implementation of `BRANCHES_SHARING_CODE` and `IF_SAME_THEN_ELSE` if the blocks are equal.
184 fn lint_same_then_else<'tcx>(
185 cx: &LateContext<'tcx>,
186 blocks: &[&Block<'tcx>],
187 has_conditional_else: bool,
188 expr: &'tcx Expr<'_>,
190 // We only lint ifs with multiple blocks
191 if blocks.len() < 2 || parent_node_is_if_expr(expr, cx) {
195 // Check if each block has shared code
196 let has_expr = blocks[0].expr.is_some();
197 let (start_eq, mut end_eq, expr_eq) = scan_block_for_eq(cx, blocks);
199 // BRANCHES_SHARING_CODE prerequisites
200 if has_conditional_else || (start_eq == 0 && end_eq == 0 && (has_expr && !expr_eq)) {
204 // Only the start is the same
205 if start_eq != 0 && end_eq == 0 && (!has_expr || !expr_eq) {
206 let block = blocks[0];
207 let start_stmts = block.stmts.split_at(start_eq).0;
209 let mut start_walker = UsedValueFinderVisitor::new(cx);
210 for stmt in start_stmts {
211 intravisit::walk_stmt(&mut start_walker, stmt);
214 emit_branches_sharing_code_lint(
219 check_for_warn_of_moved_symbol(cx, &start_walker.def_symbols, expr),
223 } else if end_eq != 0 || (has_expr && expr_eq) {
224 let block = blocks[blocks.len() - 1];
225 let (start_stmts, block_stmts) = block.stmts.split_at(start_eq);
226 let (block_stmts, end_stmts) = block_stmts.split_at(block_stmts.len() - end_eq);
229 let mut start_walker = UsedValueFinderVisitor::new(cx);
230 for stmt in start_stmts {
231 intravisit::walk_stmt(&mut start_walker, stmt);
233 let mut moved_syms = start_walker.def_symbols;
236 let mut block_walker = UsedValueFinderVisitor::new(cx);
237 for stmt in block_stmts {
238 intravisit::walk_stmt(&mut block_walker, stmt);
240 let mut block_defs = block_walker.defs;
243 let mut moved_start: Option<usize> = None;
244 let mut end_walker = UsedValueFinderVisitor::new(cx);
245 for (index, stmt) in end_stmts.iter().enumerate() {
246 intravisit::walk_stmt(&mut end_walker, stmt);
248 for value in &end_walker.uses {
249 // Well we can't move this and all prev statements. So reset
250 if block_defs.contains(value) {
251 moved_start = Some(index + 1);
252 end_walker.defs.drain().for_each(|x| {
253 block_defs.insert(x);
256 end_walker.def_symbols.clear();
260 end_walker.uses.clear();
263 if let Some(moved_start) = moved_start {
264 end_eq -= moved_start;
267 let end_linable = block.expr.map_or_else(
270 intravisit::walk_expr(&mut end_walker, expr);
271 end_walker.uses.iter().any(|x| !block_defs.contains(x))
276 end_walker.def_symbols.drain().for_each(|x| {
277 moved_syms.insert(x);
281 emit_branches_sharing_code_lint(
286 check_for_warn_of_moved_symbol(cx, &moved_syms, expr),
293 /// The return tuple is structured as follows:
294 /// 1. The amount of equal statements from the start
295 /// 2. The amount of equal statements from the end
296 /// 3. An indication if the block expressions are the same. This will also be true if both are
299 /// This function can also trigger the `IF_SAME_THEN_ELSE` in which case it'll return `(0, 0,
300 /// false)` to aboard any further processing and avoid duplicate lint triggers.
301 fn scan_block_for_eq(cx: &LateContext<'tcx>, blocks: &[&Block<'tcx>]) -> (usize, usize, bool) {
302 let mut start_eq = usize::MAX;
303 let mut end_eq = usize::MAX;
304 let mut expr_eq = true;
305 for win in blocks.windows(2) {
306 let l_stmts = win[0].stmts;
307 let r_stmts = win[1].stmts;
309 // `SpanlessEq` now keeps track of the locals and is therefore context sensitive clippy#6752.
310 // The comparison therefore needs to be done in a way that builds the correct context.
311 let mut evaluator = SpanlessEq::new(cx).enable_check_inferred_local_types();
312 let mut evaluator = evaluator.inter_expr();
314 let current_start_eq = count_eq(&mut l_stmts.iter(), &mut r_stmts.iter(), |l, r| evaluator.eq_stmt(l, r));
316 let current_end_eq = {
317 // We skip the middle statements which can't be equal
318 let end_comparison_count = l_stmts.len().min(r_stmts.len()) - current_start_eq;
319 let it1 = l_stmts.iter().skip(l_stmts.len() - end_comparison_count);
320 let it2 = r_stmts.iter().skip(r_stmts.len() - end_comparison_count);
322 .fold(0, |acc, (l, r)| if evaluator.eq_stmt(l, r) { acc + 1 } else { 0 })
324 let block_expr_eq = both(&win[0].expr, &win[1].expr, |l, r| evaluator.eq_expr(l, r));
329 if l_stmts.len() == r_stmts.len();
330 if l_stmts.len() == current_start_eq;
331 if run_lints(cx, &[IF_SAME_THEN_ELSE], win[0].hir_id);
332 if run_lints(cx, &[IF_SAME_THEN_ELSE], win[1].hir_id);
338 "this `if` has identical blocks",
343 return (0, 0, false);
347 start_eq = start_eq.min(current_start_eq);
348 end_eq = end_eq.min(current_end_eq);
349 expr_eq &= block_expr_eq;
352 let has_expr = blocks[0].expr.is_some();
353 if has_expr && !expr_eq {
357 // Check if the regions are overlapping. Set `end_eq` to prevent the overlap
358 let min_block_size = blocks.iter().map(|x| x.stmts.len()).min().unwrap();
359 if (start_eq + end_eq) > min_block_size {
360 end_eq = min_block_size - start_eq;
363 (start_eq, end_eq, expr_eq)
366 fn check_for_warn_of_moved_symbol(
367 cx: &LateContext<'tcx>,
368 symbols: &FxHashSet<Symbol>,
369 if_expr: &'tcx Expr<'_>,
371 get_enclosing_block(cx, if_expr.hir_id).map_or(false, |block| {
372 let ignore_span = block.span.shrink_to_lo().to(if_expr.span);
376 .filter(|sym| !sym.as_str().starts_with('_'))
378 let mut walker = ContainsName {
387 .filter(|stmt| !ignore_span.overlaps(stmt.span))
388 .for_each(|stmt| intravisit::walk_stmt(&mut walker, stmt));
390 if let Some(expr) = block.expr {
391 intravisit::walk_expr(&mut walker, expr);
399 fn emit_branches_sharing_code_lint(
400 cx: &LateContext<'tcx>,
404 warn_about_moved_symbol: bool,
405 blocks: &[&Block<'tcx>],
406 if_expr: &'tcx Expr<'_>,
408 if start_stmts == 0 && !lint_end {
412 // (help, span, suggestion)
413 let mut suggestions: Vec<(&str, Span, String)> = vec![];
414 let mut add_expr_note = false;
416 // Construct suggestions
418 let block = blocks[0];
419 let span_start = first_line_of_span(cx, if_expr.span).shrink_to_lo();
420 let span_end = block.stmts[start_stmts - 1].span.source_callsite();
422 let cond_span = first_line_of_span(cx, if_expr.span).until(block.span);
423 let cond_snippet = reindent_multiline(snippet(cx, cond_span, "_"), false, None);
424 let cond_indent = indent_of(cx, cond_span);
425 let moved_span = block.stmts[0].span.source_callsite().to(span_end);
426 let moved_snippet = reindent_multiline(snippet(cx, moved_span, "_"), true, None);
427 let suggestion = moved_snippet.to_string() + "\n" + &cond_snippet + "{";
428 let suggestion = reindent_multiline(Cow::Borrowed(&suggestion), true, cond_indent);
430 let span = span_start.to(span_end);
431 suggestions.push(("start", span, suggestion.to_string()));
435 let block = blocks[blocks.len() - 1];
436 let span_end = block.span.shrink_to_hi();
438 let moved_start = if end_stmts == 0 && block.expr.is_some() {
439 block.expr.unwrap().span
441 block.stmts[block.stmts.len() - end_stmts].span
444 let moved_end = block
446 .map_or_else(|| block.stmts[block.stmts.len() - 1].span, |expr| expr.span)
449 let moved_span = moved_start.to(moved_end);
450 let moved_snipped = reindent_multiline(snippet(cx, moved_span, "_"), true, None);
451 let indent = indent_of(cx, if_expr.span.shrink_to_hi());
452 let suggestion = "}\n".to_string() + &moved_snipped;
453 let suggestion = reindent_multiline(Cow::Borrowed(&suggestion), true, indent);
455 let mut span = moved_start.to(span_end);
456 // Improve formatting if the inner block has indention (i.e. normal Rust formatting)
457 let test_span = Span::new(span.lo() - BytePos(4), span.lo(), span.ctxt());
458 if snippet_opt(cx, test_span)
459 .map(|snip| snip == " ")
462 span = span.with_lo(test_span.lo());
465 suggestions.push(("end", span, suggestion.to_string()));
466 add_expr_note = !cx.typeck_results().expr_ty(if_expr).is_unit()
469 let add_optional_msgs = |diag: &mut DiagnosticBuilder<'_>| {
471 diag.note("The end suggestion probably needs some adjustments to use the expression result correctly");
474 if warn_about_moved_symbol {
475 diag.warn("Some moved values might need to be renamed to avoid wrong references");
480 if suggestions.len() == 1 {
481 let (place_str, span, sugg) = suggestions.pop().unwrap();
482 let msg = format!("all if blocks contain the same code at the {}", place_str);
483 let help = format!("consider moving the {} statements out like this", place_str);
484 span_lint_and_then(cx, BRANCHES_SHARING_CODE, span, msg.as_str(), |diag| {
485 diag.span_suggestion(span, help.as_str(), sugg, Applicability::Unspecified);
487 add_optional_msgs(diag);
489 } else if suggestions.len() == 2 {
490 let (_, end_span, end_sugg) = suggestions.pop().unwrap();
491 let (_, start_span, start_sugg) = suggestions.pop().unwrap();
494 BRANCHES_SHARING_CODE,
496 "all if blocks contain the same code at the start and the end. Here at the start",
498 diag.span_note(end_span, "and here at the end");
500 diag.span_suggestion(
502 "consider moving the start statements out like this",
504 Applicability::Unspecified,
507 diag.span_suggestion(
509 "and consider moving the end statements out like this",
511 Applicability::Unspecified,
514 add_optional_msgs(diag);
520 /// This visitor collects `HirId`s and Symbols of defined symbols and `HirId`s of used values.
521 struct UsedValueFinderVisitor<'a, 'tcx> {
522 cx: &'a LateContext<'tcx>,
524 /// The `HirId`s of defined values in the scanned statements
525 defs: FxHashSet<HirId>,
527 /// The Symbols of the defined symbols in the scanned statements
528 def_symbols: FxHashSet<Symbol>,
530 /// The `HirId`s of the used values
531 uses: FxHashSet<HirId>,
534 impl<'a, 'tcx> UsedValueFinderVisitor<'a, 'tcx> {
535 fn new(cx: &'a LateContext<'tcx>) -> Self {
536 UsedValueFinderVisitor {
538 defs: FxHashSet::default(),
539 def_symbols: FxHashSet::default(),
540 uses: FxHashSet::default(),
545 impl<'a, 'tcx> Visitor<'tcx> for UsedValueFinderVisitor<'a, 'tcx> {
546 type Map = Map<'tcx>;
548 fn nested_visit_map(&mut self) -> NestedVisitorMap<Self::Map> {
549 NestedVisitorMap::All(self.cx.tcx.hir())
552 fn visit_local(&mut self, l: &'tcx rustc_hir::Local<'tcx>) {
553 let local_id = l.pat.hir_id;
554 self.defs.insert(local_id);
556 if let Some(sym) = l.pat.simple_ident() {
557 self.def_symbols.insert(sym.name);
560 if let Some(expr) = l.init {
561 intravisit::walk_expr(self, expr);
565 fn visit_qpath(&mut self, qpath: &'tcx rustc_hir::QPath<'tcx>, id: HirId, _span: rustc_span::Span) {
566 if let rustc_hir::QPath::Resolved(_, path) = *qpath {
567 if path.segments.len() == 1 {
568 if let rustc_hir::def::Res::Local(var) = self.cx.qpath_res(qpath, id) {
569 self.uses.insert(var);
576 /// Implementation of `IFS_SAME_COND`.
577 fn lint_same_cond(cx: &LateContext<'_>, conds: &[&Expr<'_>]) {
578 let hash: &dyn Fn(&&Expr<'_>) -> u64 = &|expr| -> u64 {
579 let mut h = SpanlessHash::new(cx);
584 let eq: &dyn Fn(&&Expr<'_>, &&Expr<'_>) -> bool = &|&lhs, &rhs| -> bool { eq_expr_value(cx, lhs, rhs) };
586 for (i, j) in search_same(conds, hash, eq) {
591 "this `if` has the same condition as a previous `if`",
598 /// Implementation of `SAME_FUNCTIONS_IN_IF_CONDITION`.
599 fn lint_same_fns_in_if_cond(cx: &LateContext<'_>, conds: &[&Expr<'_>]) {
600 let hash: &dyn Fn(&&Expr<'_>) -> u64 = &|expr| -> u64 {
601 let mut h = SpanlessHash::new(cx);
606 let eq: &dyn Fn(&&Expr<'_>, &&Expr<'_>) -> bool = &|&lhs, &rhs| -> bool {
607 // Do not lint if any expr originates from a macro
608 if in_macro(lhs.span) || in_macro(rhs.span) {
611 // Do not spawn warning if `IFS_SAME_COND` already produced it.
612 if eq_expr_value(cx, lhs, rhs) {
615 SpanlessEq::new(cx).eq_expr(lhs, rhs)
618 for (i, j) in search_same(conds, hash, eq) {
621 SAME_FUNCTIONS_IN_IF_CONDITION,
623 "this `if` has the same function call as a previous `if`",