From a6f54f5dfdfdf0017ffecfbcd6f43352b8b71ca1 Mon Sep 17 00:00:00 2001 From: xFrednet Date: Thu, 1 Apr 2021 18:30:47 +0200 Subject: Renaming the lint to branches_sharing_code and fixing typos --- CHANGELOG.md | 2 +- clippy_lints/src/copies.rs | 35 ++-- clippy_lints/src/lib.rs | 5 +- clippy_utils/src/lib.rs | 2 +- tests/ui/branches_sharing_code/shared_at_bottom.rs | 209 +++++++++++++++++++++ .../branches_sharing_code/shared_at_bottom.stderr | 143 ++++++++++++++ tests/ui/branches_sharing_code/shared_at_top.rs | 103 ++++++++++ .../ui/branches_sharing_code/shared_at_top.stderr | 121 ++++++++++++ .../shared_at_top_and_bottom.rs | 119 ++++++++++++ .../shared_at_top_and_bottom.stderr | 154 +++++++++++++++ tests/ui/branches_sharing_code/valid_if_blocks.rs | 155 +++++++++++++++ .../branches_sharing_code/valid_if_blocks.stderr | 101 ++++++++++ tests/ui/checked_unwrap/complex_conditionals.rs | 2 +- .../checked_unwrap/complex_conditionals_nested.rs | 2 +- tests/ui/checked_unwrap/simple_conditionals.rs | 2 +- tests/ui/default_numeric_fallback.rs | 2 +- tests/ui/if_same_then_else.rs | 2 +- tests/ui/if_same_then_else2.rs | 2 +- tests/ui/let_if_seq.rs | 2 +- tests/ui/needless_bool/simple.rs | 2 +- tests/ui/needless_return.fixed | 2 +- tests/ui/needless_return.rs | 2 +- tests/ui/shared_code_in_if_blocks/shared_at_bot.rs | 209 --------------------- .../shared_code_in_if_blocks/shared_at_bot.stderr | 143 -------------- tests/ui/shared_code_in_if_blocks/shared_at_top.rs | 103 ---------- .../shared_code_in_if_blocks/shared_at_top.stderr | 121 ------------ .../shared_at_top_and_bot.rs | 119 ------------ .../shared_at_top_and_bot.stderr | 154 --------------- .../ui/shared_code_in_if_blocks/valid_if_blocks.rs | 155 --------------- .../valid_if_blocks.stderr | 101 ---------- 30 files changed, 1138 insertions(+), 1136 deletions(-) create mode 100644 tests/ui/branches_sharing_code/shared_at_bottom.rs create mode 100644 tests/ui/branches_sharing_code/shared_at_bottom.stderr create mode 100644 tests/ui/branches_sharing_code/shared_at_top.rs create mode 100644 tests/ui/branches_sharing_code/shared_at_top.stderr create mode 100644 tests/ui/branches_sharing_code/shared_at_top_and_bottom.rs create mode 100644 tests/ui/branches_sharing_code/shared_at_top_and_bottom.stderr create mode 100644 tests/ui/branches_sharing_code/valid_if_blocks.rs create mode 100644 tests/ui/branches_sharing_code/valid_if_blocks.stderr delete mode 100644 tests/ui/shared_code_in_if_blocks/shared_at_bot.rs delete mode 100644 tests/ui/shared_code_in_if_blocks/shared_at_bot.stderr delete mode 100644 tests/ui/shared_code_in_if_blocks/shared_at_top.rs delete mode 100644 tests/ui/shared_code_in_if_blocks/shared_at_top.stderr delete mode 100644 tests/ui/shared_code_in_if_blocks/shared_at_top_and_bot.rs delete mode 100644 tests/ui/shared_code_in_if_blocks/shared_at_top_and_bot.stderr delete mode 100644 tests/ui/shared_code_in_if_blocks/valid_if_blocks.rs delete mode 100644 tests/ui/shared_code_in_if_blocks/valid_if_blocks.stderr diff --git a/CHANGELOG.md b/CHANGELOG.md index 790bca4ac18..73997192ae0 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2129,6 +2129,7 @@ Released 2018-09-13 [`borrowed_box`]: https://rust-lang.github.io/rust-clippy/master/index.html#borrowed_box [`box_vec`]: https://rust-lang.github.io/rust-clippy/master/index.html#box_vec [`boxed_local`]: https://rust-lang.github.io/rust-clippy/master/index.html#boxed_local +[`branches_sharing_code`]: https://rust-lang.github.io/rust-clippy/master/index.html#branches_sharing_code [`builtin_type_shadow`]: https://rust-lang.github.io/rust-clippy/master/index.html#builtin_type_shadow [`bytes_nth`]: https://rust-lang.github.io/rust-clippy/master/index.html#bytes_nth [`cargo_common_metadata`]: https://rust-lang.github.io/rust-clippy/master/index.html#cargo_common_metadata @@ -2455,7 +2456,6 @@ Released 2018-09-13 [`shadow_reuse`]: https://rust-lang.github.io/rust-clippy/master/index.html#shadow_reuse [`shadow_same`]: https://rust-lang.github.io/rust-clippy/master/index.html#shadow_same [`shadow_unrelated`]: https://rust-lang.github.io/rust-clippy/master/index.html#shadow_unrelated -[`shared_code_in_if_blocks`]: https://rust-lang.github.io/rust-clippy/master/index.html#shared_code_in_if_blocks [`short_circuit_statement`]: https://rust-lang.github.io/rust-clippy/master/index.html#short_circuit_statement [`should_assert_eq`]: https://rust-lang.github.io/rust-clippy/master/index.html#should_assert_eq [`should_implement_trait`]: https://rust-lang.github.io/rust-clippy/master/index.html#should_implement_trait diff --git a/clippy_lints/src/copies.rs b/clippy_lints/src/copies.rs index ff99a9a09b3..1b982221b06 100644 --- a/clippy_lints/src/copies.rs +++ b/clippy_lints/src/copies.rs @@ -1,7 +1,8 @@ -use crate::utils::{ - both, count_eq, eq_expr_value, first_line_of_span, get_enclosing_block, get_parent_expr, if_sequence, in_macro, - indent_of, parent_node_is_if_expr, reindent_multiline, run_lints, search_same, snippet, snippet_opt, - span_lint_and_note, span_lint_and_then, ContainsName, SpanlessEq, SpanlessHash, +use clippy_utils::diagnostics::{span_lint_and_note, span_lint_and_then}; +use clippy_utils::source::{first_line_of_span, indent_of, reindent_multiline, snippet, snippet_opt}; +use clippy_utils::{ + both, count_eq, eq_expr_value, get_enclosing_block, get_parent_expr, if_sequence, in_macro, parent_node_is_if_expr, + run_lints, search_same, ContainsName, SpanlessEq, SpanlessHash, }; use if_chain::if_chain; use rustc_data_structures::fx::FxHashSet; @@ -141,7 +142,7 @@ declare_clippy_lint! { /// 42 /// }; /// ``` - pub SHARED_CODE_IN_IF_BLOCKS, + pub BRANCHES_SHARING_CODE, complexity, "`if` statement with shared code in all blocks" } @@ -150,7 +151,7 @@ declare_lint_pass!(CopyAndPaste => [ IFS_SAME_COND, SAME_FUNCTIONS_IN_IF_CONDITION, IF_SAME_THEN_ELSE, - SHARED_CODE_IN_IF_BLOCKS + BRANCHES_SHARING_CODE ]); impl<'tcx> LateLintPass<'tcx> for CopyAndPaste { @@ -173,17 +174,17 @@ impl<'tcx> LateLintPass<'tcx> for CopyAndPaste { lint_same_cond(cx, &conds); lint_same_fns_in_if_cond(cx, &conds); // Block duplication - lint_same_then_else(cx, &blocks, conds.len() != blocks.len(), expr); + lint_same_then_else(cx, &blocks, conds.len() == blocks.len(), expr); } } } } -/// Implementation of `SHARED_CODE_IN_IF_BLOCKS` and `IF_SAME_THEN_ELSE` if the blocks are equal. +/// Implementation of `BRANCHES_SHARING_CODE` and `IF_SAME_THEN_ELSE` if the blocks are equal. fn lint_same_then_else<'tcx>( cx: &LateContext<'tcx>, blocks: &[&Block<'tcx>], - has_unconditional_else: bool, + has_conditional_else: bool, expr: &'tcx Expr<'_>, ) { // We only lint ifs with multiple blocks @@ -195,8 +196,8 @@ fn lint_same_then_else<'tcx>( let has_expr = blocks[0].expr.is_some(); let (start_eq, mut end_eq, expr_eq) = scan_block_for_eq(cx, blocks); - // SHARED_CODE_IN_IF_BLOCKS prerequisites - if !has_unconditional_else || (start_eq == 0 && end_eq == 0 && (has_expr && !expr_eq)) { + // BRANCHES_SHARING_CODE prerequisites + if has_conditional_else || (start_eq == 0 && end_eq == 0 && (has_expr && !expr_eq)) { return; } @@ -210,7 +211,7 @@ fn lint_same_then_else<'tcx>( intravisit::walk_stmt(&mut start_walker, stmt); } - emit_shared_code_in_if_blocks_lint( + emit_branches_sharing_code_lint( cx, start_eq, 0, @@ -277,7 +278,7 @@ fn lint_same_then_else<'tcx>( }); } - emit_shared_code_in_if_blocks_lint( + emit_branches_sharing_code_lint( cx, start_eq, end_eq, @@ -298,7 +299,7 @@ fn scan_block_for_eq(cx: &LateContext<'tcx>, blocks: &[&Block<'tcx>]) -> (usize, let r_stmts = win[1].stmts; // `SpanlessEq` now keeps track of the locals and is therefore context sensitive clippy#6752. - // The comparison therefor needs to be done in a way that builds the correct context. + // The comparison therefore needs to be done in a way that builds the correct context. let mut evaluator = SpanlessEq::new(cx); let mut evaluator = evaluator.inter_expr(); @@ -387,7 +388,7 @@ fn check_for_warn_of_moved_symbol( }) } -fn emit_shared_code_in_if_blocks_lint( +fn emit_branches_sharing_code_lint( cx: &LateContext<'tcx>, start_stmts: usize, end_stmts: usize, @@ -472,7 +473,7 @@ fn emit_shared_code_in_if_blocks_lint( let (place_str, span, sugg) = suggestions.pop().unwrap(); let msg = format!("all if blocks contain the same code at the {}", place_str); let help = format!("consider moving the {} statements out like this", place_str); - span_lint_and_then(cx, SHARED_CODE_IN_IF_BLOCKS, span, msg.as_str(), |diag| { + span_lint_and_then(cx, BRANCHES_SHARING_CODE, span, msg.as_str(), |diag| { diag.span_suggestion(span, help.as_str(), sugg, Applicability::Unspecified); add_optional_msgs(diag); @@ -482,7 +483,7 @@ fn emit_shared_code_in_if_blocks_lint( let (_, start_span, start_sugg) = suggestions.pop().unwrap(); span_lint_and_then( cx, - SHARED_CODE_IN_IF_BLOCKS, + BRANCHES_SHARING_CODE, start_span, "all if blocks contain the same code at the start and the end. Here at the start", move |diag| { diff --git a/clippy_lints/src/lib.rs b/clippy_lints/src/lib.rs index 3f5c1c7c526..43e89c2475f 100644 --- a/clippy_lints/src/lib.rs +++ b/clippy_lints/src/lib.rs @@ -613,10 +613,10 @@ pub fn register_plugins(store: &mut rustc_lint::LintStore, sess: &Session, conf: &collapsible_if::COLLAPSIBLE_IF, &collapsible_match::COLLAPSIBLE_MATCH, &comparison_chain::COMPARISON_CHAIN, + &copies::BRANCHES_SHARING_CODE, &copies::IFS_SAME_COND, &copies::IF_SAME_THEN_ELSE, &copies::SAME_FUNCTIONS_IN_IF_CONDITION, - &copies::SHARED_CODE_IN_IF_BLOCKS, ©_iterator::COPY_ITERATOR, &create_dir::CREATE_DIR, &dbg_macro::DBG_MACRO, @@ -1483,9 +1483,9 @@ pub fn register_plugins(store: &mut rustc_lint::LintStore, sess: &Session, conf: LintId::of(&collapsible_if::COLLAPSIBLE_IF), LintId::of(&collapsible_match::COLLAPSIBLE_MATCH), LintId::of(&comparison_chain::COMPARISON_CHAIN), + LintId::of(&copies::BRANCHES_SHARING_CODE), LintId::of(&copies::IFS_SAME_COND), LintId::of(&copies::IF_SAME_THEN_ELSE), - LintId::of(&copies::SHARED_CODE_IN_IF_BLOCKS), LintId::of(&default::FIELD_REASSIGN_WITH_DEFAULT), LintId::of(&derive::DERIVE_HASH_XOR_EQ), LintId::of(&derive::DERIVE_ORD_XOR_PARTIAL_ORD), @@ -1872,6 +1872,7 @@ pub fn register_plugins(store: &mut rustc_lint::LintStore, sess: &Session, conf: LintId::of(&booleans::NONMINIMAL_BOOL), LintId::of(&casts::CHAR_LIT_AS_U8), LintId::of(&casts::UNNECESSARY_CAST), + LintId::of(&copies::BRANCHES_SHARING_CODE), LintId::of(&double_comparison::DOUBLE_COMPARISONS), LintId::of(&double_parens::DOUBLE_PARENS), LintId::of(&duration_subsec::DURATION_SUBSEC), diff --git a/clippy_utils/src/lib.rs b/clippy_utils/src/lib.rs index f9576068dd6..7ce90ffd9f0 100644 --- a/clippy_utils/src/lib.rs +++ b/clippy_utils/src/lib.rs @@ -47,7 +47,7 @@ pub mod usage; pub mod visitors; pub use self::attrs::*; -pub use self::hir_utils::{both, eq_expr_value, over, SpanlessEq, SpanlessHash}; +pub use self::hir_utils::{both, count_eq, eq_expr_value, over, SpanlessEq, SpanlessHash}; use std::collections::hash_map::Entry; use std::hash::BuildHasherDefault; diff --git a/tests/ui/branches_sharing_code/shared_at_bottom.rs b/tests/ui/branches_sharing_code/shared_at_bottom.rs new file mode 100644 index 00000000000..c389c243d44 --- /dev/null +++ b/tests/ui/branches_sharing_code/shared_at_bottom.rs @@ -0,0 +1,209 @@ +#![allow(dead_code)] +#![deny(clippy::if_same_then_else, clippy::branches_sharing_code)] + +// This tests the branches_sharing_code lint at the end of blocks + +fn simple_examples() { + let x = 1; + + let _ = if x == 7 { + println!("Branch I"); + let start_value = 0; + println!("=^.^="); + + // Same but not moveable due to `start_value` + let _ = start_value; + + // The rest is self contained and moveable => Only lint the rest + let result = false; + println!("Block end!"); + result + } else { + println!("Branch II"); + let start_value = 8; + println!("xD"); + + // Same but not moveable due to `start_value` + let _ = start_value; + + // The rest is self contained and moveable => Only lint the rest + let result = false; + println!("Block end!"); + result + }; + + // Else if block + if x == 9 { + println!("The index is: 6"); + + println!("Same end of block"); + } else if x == 8 { + println!("The index is: 4"); + + // We should only get a lint trigger for the last statement + println!("This is also eq with the else block"); + println!("Same end of block"); + } else { + println!("This is also eq with the else block"); + println!("Same end of block"); + } + + // Use of outer scope value + let outer_scope_value = "I'm outside the if block"; + if x < 99 { + let z = "How are you"; + println!("I'm a local because I use the value `z`: `{}`", z); + + println!( + "I'm moveable because I know: `outer_scope_value`: '{}'", + outer_scope_value + ); + } else { + let z = 45678000; + println!("I'm a local because I use the value `z`: `{}`", z); + + println!( + "I'm moveable because I know: `outer_scope_value`: '{}'", + outer_scope_value + ); + } + + if x == 9 { + if x == 8 { + // No parent!! + println!("---"); + println!("Hello World"); + } else { + println!("Hello World"); + } + } +} + +/// Simple examples where the move can cause some problems due to moved values +fn simple_but_suggestion_is_invalid() { + let x = 16; + + // Local value + let later_used_value = 17; + if x == 9 { + let _ = 9; + let later_used_value = "A string value"; + println!("{}", later_used_value); + } else { + let later_used_value = "A string value"; + println!("{}", later_used_value); + // I'm expecting a note about this + } + println!("{}", later_used_value); + + // outer function + if x == 78 { + let simple_examples = "I now identify as a &str :)"; + println!("This is the new simple_example: {}", simple_examples); + } else { + println!("Separator print statement"); + + let simple_examples = "I now identify as a &str :)"; + println!("This is the new simple_example: {}", simple_examples); + } + simple_examples(); +} + +/// Tests where the blocks are not linted due to the used value scope +fn not_moveable_due_to_value_scope() { + let x = 18; + + // Using a local value in the moved code + if x == 9 { + let y = 18; + println!("y is: `{}`", y); + } else { + let y = "A string"; + println!("y is: `{}`", y); + } + + // Using a local value in the expression + let _ = if x == 0 { + let mut result = x + 1; + + println!("1. Doing some calculations"); + println!("2. Some more calculations"); + println!("3. Setting result"); + + result + } else { + let mut result = x - 1; + + println!("1. Doing some calculations"); + println!("2. Some more calculations"); + println!("3. Setting result"); + + result + }; + + let _ = if x == 7 { + let z1 = 100; + println!("z1: {}", z1); + + let z2 = z1; + println!("z2: {}", z2); + + z2 + } else { + let z1 = 300; + println!("z1: {}", z1); + + let z2 = z1; + println!("z2: {}", z2); + + z2 + }; +} + +/// This should add a note to the lint msg since the moved expression is not `()` +fn added_note_for_expression_use() -> u32 { + let x = 9; + + let _ = if x == 7 { + x << 2 + } else { + let _ = 6; + x << 2 + }; + + if x == 9 { + x * 4 + } else { + let _ = 17; + x * 4 + } +} + +#[rustfmt::skip] +fn test_suggestion_with_weird_formatting() { + let x = 9; + let mut a = 0; + let mut b = 0; + + // The error message still looks weird tbh but this is the best I can do + // for weird formatting + if x == 17 { b = 1; a = 0x99; } else { a = 0x99; } +} + +fn fp_test() { + let x = 17; + + if x == 18 { + let y = 19; + if y < x { + println!("Trigger") + } + } else { + let z = 166; + if z < x { + println!("Trigger") + } + } +} + +fn main() {} diff --git a/tests/ui/branches_sharing_code/shared_at_bottom.stderr b/tests/ui/branches_sharing_code/shared_at_bottom.stderr new file mode 100644 index 00000000000..271fcd8b6c1 --- /dev/null +++ b/tests/ui/branches_sharing_code/shared_at_bottom.stderr @@ -0,0 +1,143 @@ +error: all if blocks contain the same code at the end + --> $DIR/shared_at_bottom.rs:30:5 + | +LL | / let result = false; +LL | | println!("Block end!"); +LL | | result +LL | | }; + | |_____^ + | +note: the lint level is defined here + --> $DIR/shared_at_bottom.rs:2:36 + | +LL | #![deny(clippy::if_same_then_else, clippy::branches_sharing_code)] + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + = note: The end suggestion probably needs some adjustments to use the expression result correctly +help: consider moving the end statements out like this + | +LL | } +LL | let result = false; +LL | println!("Block end!"); +LL | result; + | + +error: all if blocks contain the same code at the end + --> $DIR/shared_at_bottom.rs:48:5 + | +LL | / println!("Same end of block"); +LL | | } + | |_____^ + | +help: consider moving the end statements out like this + | +LL | } +LL | println!("Same end of block"); + | + +error: all if blocks contain the same code at the end + --> $DIR/shared_at_bottom.rs:65:5 + | +LL | / println!( +LL | | "I'm moveable because I know: `outer_scope_value`: '{}'", +LL | | outer_scope_value +LL | | ); +LL | | } + | |_____^ + | +help: consider moving the end statements out like this + | +LL | } +LL | println!( +LL | "I'm moveable because I know: `outer_scope_value`: '{}'", +LL | outer_scope_value +LL | ); + | + +error: all if blocks contain the same code at the end + --> $DIR/shared_at_bottom.rs:77:9 + | +LL | / println!("Hello World"); +LL | | } + | |_________^ + | +help: consider moving the end statements out like this + | +LL | } +LL | println!("Hello World"); + | + +error: all if blocks contain the same code at the end + --> $DIR/shared_at_bottom.rs:93:5 + | +LL | / let later_used_value = "A string value"; +LL | | println!("{}", later_used_value); +LL | | // I'm expecting a note about this +LL | | } + | |_____^ + | + = warning: Some moved values might need to be renamed to avoid wrong references +help: consider moving the end statements out like this + | +LL | } +LL | let later_used_value = "A string value"; +LL | println!("{}", later_used_value); + | + +error: all if blocks contain the same code at the end + --> $DIR/shared_at_bottom.rs:106:5 + | +LL | / let simple_examples = "I now identify as a &str :)"; +LL | | println!("This is the new simple_example: {}", simple_examples); +LL | | } + | |_____^ + | + = warning: Some moved values might need to be renamed to avoid wrong references +help: consider moving the end statements out like this + | +LL | } +LL | let simple_examples = "I now identify as a &str :)"; +LL | println!("This is the new simple_example: {}", simple_examples); + | + +error: all if blocks contain the same code at the end + --> $DIR/shared_at_bottom.rs:171:5 + | +LL | / x << 2 +LL | | }; + | |_____^ + | + = note: The end suggestion probably needs some adjustments to use the expression result correctly +help: consider moving the end statements out like this + | +LL | } +LL | x << 2; + | + +error: all if blocks contain the same code at the end + --> $DIR/shared_at_bottom.rs:178:5 + | +LL | / x * 4 +LL | | } + | |_____^ + | + = note: The end suggestion probably needs some adjustments to use the expression result correctly +help: consider moving the end statements out like this + | +LL | } +LL | x * 4 + | + +error: all if blocks contain the same code at the end + --> $DIR/shared_at_bottom.rs:190:44 + | +LL | if x == 17 { b = 1; a = 0x99; } else { a = 0x99; } + | ^^^^^^^^^^^ + | +help: consider moving the end statements out like this + | +LL | if x == 17 { b = 1; a = 0x99; } else { } +LL | a = 0x99; + | + +error: aborting due to 9 previous errors + diff --git a/tests/ui/branches_sharing_code/shared_at_top.rs b/tests/ui/branches_sharing_code/shared_at_top.rs new file mode 100644 index 00000000000..e65bcfd7873 --- /dev/null +++ b/tests/ui/branches_sharing_code/shared_at_top.rs @@ -0,0 +1,103 @@ +#![allow(dead_code, clippy::eval_order_dependence)] +#![deny(clippy::if_same_then_else, clippy::branches_sharing_code)] + +// This tests the branches_sharing_code lint at the start of blocks + +fn simple_examples() { + let x = 0; + + // Simple + if true { + println!("Hello World!"); + println!("I'm branch nr: 1"); + } else { + println!("Hello World!"); + println!("I'm branch nr: 2"); + } + + // Else if + if x == 0 { + let y = 9; + println!("The value y was set to: `{}`", y); + let _z = y; + + println!("I'm the true start index of arrays"); + } else if x == 1 { + let y = 9; + println!("The value y was set to: `{}`", y); + let _z = y; + + println!("I start counting from 1 so my array starts from `1`"); + } else { + let y = 9; + println!("The value y was set to: `{}`", y); + let _z = y; + + println!("Ha, Pascal allows you to start the array where you want") + } + + // Return a value + let _ = if x == 7 { + let y = 16; + println!("What can I say except: \"you're welcome?\""); + let _ = y; + x + } else { + let y = 16; + println!("Thank you"); + y + }; +} + +/// Simple examples where the move can cause some problems due to moved values +fn simple_but_suggestion_is_invalid() { + let x = 10; + + // Can't be automatically moved because used_value_name is getting used again + let used_value_name = 19; + if x == 10 { + let used_value_name = "Different type"; + println!("Str: {}", used_value_name); + let _ = 1; + } else { + let used_value_name = "Different type"; + println!("Str: {}", used_value_name); + let _ = 2; + } + let _ = used_value_name; + + // This can be automatically moved as `can_be_overridden` is not used again + let can_be_overridden = 8; + let _ = can_be_overridden; + if x == 11 { + let can_be_overridden = "Move me"; + println!("I'm also moveable"); + let _ = 111; + } else { + let can_be_overridden = "Move me"; + println!("I'm also moveable"); + let _ = 222; + } +} + +/// This function tests that the `IS_SAME_THAN_ELSE` only covers the lint if it's enabled. +fn check_if_same_than_else_mask() { + let x = 2021; + + #[allow(clippy::if_same_then_else)] + if x == 2020 { + println!("This should trigger the `SHARED_CODE_IN_IF_BLOCKS` lint."); + println!("Because `IF_SAME_THEN_ELSE` is allowed here"); + } else { + println!("This should trigger the `SHARED_CODE_IN_IF_BLOCKS` lint."); + println!("Because `IF_SAME_THEN_ELSE` is allowed here"); + } + + if x == 2019 { + println!("This should trigger `IS_SAME_THAN_ELSE` as usual"); + } else { + println!("This should trigger `IS_SAME_THAN_ELSE` as usual"); + } +} + +fn main() {} diff --git a/tests/ui/branches_sharing_code/shared_at_top.stderr b/tests/ui/branches_sharing_code/shared_at_top.stderr new file mode 100644 index 00000000000..15867e9ea02 --- /dev/null +++ b/tests/ui/branches_sharing_code/shared_at_top.stderr @@ -0,0 +1,121 @@ +error: all if blocks contain the same code at the start + --> $DIR/shared_at_top.rs:10:5 + | +LL | / if true { +LL | | println!("Hello World!"); + | |_________________________________^ + | +note: the lint level is defined here + --> $DIR/shared_at_top.rs:2:36 + | +LL | #![deny(clippy::if_same_then_else, clippy::branches_sharing_code)] + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ +help: consider moving the start statements out like this + | +LL | println!("Hello World!"); +LL | if true { + | + +error: all if blocks contain the same code at the start + --> $DIR/shared_at_top.rs:19:5 + | +LL | / if x == 0 { +LL | | let y = 9; +LL | | println!("The value y was set to: `{}`", y); +LL | | let _z = y; + | |___________________^ + | + = warning: Some moved values might need to be renamed to avoid wrong references +help: consider moving the start statements out like this + | +LL | let y = 9; +LL | println!("The value y was set to: `{}`", y); +LL | let _z = y; +LL | if x == 0 { + | + +error: all if blocks contain the same code at the start + --> $DIR/shared_at_top.rs:40:5 + | +LL | / let _ = if x == 7 { +LL | | let y = 16; + | |___________________^ + | +help: consider moving the start statements out like this + | +LL | let y = 16; +LL | let _ = if x == 7 { + | + +error: all if blocks contain the same code at the start + --> $DIR/shared_at_top.rs:58:5 + | +LL | / if x == 10 { +LL | | let used_value_name = "Different type"; +LL | | println!("Str: {}", used_value_name); + | |_____________________________________________^ + | + = warning: Some moved values might need to be renamed to avoid wrong references +help: consider moving the start statements out like this + | +LL | let used_value_name = "Different type"; +LL | println!("Str: {}", used_value_name); +LL | if x == 10 { + | + +error: all if blocks contain the same code at the start + --> $DIR/shared_at_top.rs:72:5 + | +LL | / if x == 11 { +LL | | let can_be_overridden = "Move me"; +LL | | println!("I'm also moveable"); + | |______________________________________^ + | + = warning: Some moved values might need to be renamed to avoid wrong references +help: consider moving the start statements out like this + | +LL | let can_be_overridden = "Move me"; +LL | println!("I'm also moveable"); +LL | if x == 11 { + | + +error: all if blocks contain the same code at the start + --> $DIR/shared_at_top.rs:88:5 + | +LL | / if x == 2020 { +LL | | println!("This should trigger the `SHARED_CODE_IN_IF_BLOCKS` lint."); +LL | | println!("Because `IF_SAME_THEN_ELSE` is allowed here"); + | |________________________________________________________________^ + | +help: consider moving the start statements out like this + | +LL | println!("This should trigger the `SHARED_CODE_IN_IF_BLOCKS` lint."); +LL | println!("Because `IF_SAME_THEN_ELSE` is allowed here"); +LL | if x == 2020 { + | + +error: this `if` has identical blocks + --> $DIR/shared_at_top.rs:96:18 + | +LL | if x == 2019 { + | __________________^ +LL | | println!("This should trigger `IS_SAME_THAN_ELSE` as usual"); +LL | | } else { + | |_____^ + | +note: the lint level is defined here + --> $DIR/shared_at_top.rs:2:9 + | +LL | #![deny(clippy::if_same_then_else, clippy::branches_sharing_code)] + | ^^^^^^^^^^^^^^^^^^^^^^^^^ +note: same as this + --> $DIR/shared_at_top.rs:98:12 + | +LL | } else { + | ____________^ +LL | | println!("This should trigger `IS_SAME_THAN_ELSE` as usual"); +LL | | } + | |_____^ + +error: aborting due to 7 previous errors + diff --git a/tests/ui/branches_sharing_code/shared_at_top_and_bottom.rs b/tests/ui/branches_sharing_code/shared_at_top_and_bottom.rs new file mode 100644 index 00000000000..deefdad32c9 --- /dev/null +++ b/tests/ui/branches_sharing_code/shared_at_top_and_bottom.rs @@ -0,0 +1,119 @@ +#![allow(dead_code)] +#![deny(clippy::if_same_then_else, clippy::branches_sharing_code)] + +// branches_sharing_code at the top and bottom of the if blocks + +struct DataPack { + id: u32, + name: String, + some_data: Vec, +} + +fn overlapping_eq_regions() { + let x = 9; + + // Overlap with separator + if x == 7 { + let t = 7; + let _overlap_start = t * 2; + let _overlap_end = 2 * t; + let _u = 9; + } else { + let t = 7; + let _overlap_start = t * 2; + let _overlap_end = 2 * t; + println!("Overlap separator"); + let _overlap_start = t * 2; + let _overlap_end = 2 * t; + let _u = 9; + } + + // Overlap with separator + if x == 99 { + let r = 7; + let _overlap_start = r; + let _overlap_middle = r * r; + let _overlap_end = r * r * r; + let z = "end"; + } else { + let r = 7; + let _overlap_start = r; + let _overlap_middle = r * r; + let _overlap_middle = r * r; + let _overlap_end = r * r * r; + let z = "end"; + } +} + +fn complexer_example() { + fn gen_id(x: u32, y: u32) -> u32 { + let x = x & 0x0000_ffff; + let y = (y & 0xffff_0000) << 16; + x | y + } + + fn process_data(data: DataPack) { + let _ = data; + } + + let x = 8; + let y = 9; + if (x > 7 && y < 13) || (x + y) % 2 == 1 { + let a = 0xcafe; + let b = 0xffff00ff; + let e_id = gen_id(a, b); + + println!("From the a `{}` to the b `{}`", a, b); + + let pack = DataPack { + id: e_id, + name: "Player 1".to_string(), + some_data: vec![0x12, 0x34, 0x56, 0x78, 0x90], + }; + process_data(pack); + } else { + let a = 0xcafe; + let b = 0xffff00ff; + let e_id = gen_id(a, b); + + println!("The new ID is '{}'", e_id); + + let pack = DataPack { + id: e_id, + name: "Player 1".to_string(), + some_data: vec![0x12, 0x34, 0x56, 0x78, 0x90], + }; + process_data(pack); + } +} + +/// This should add a note to the lint msg since the moved expression is not `()` +fn added_note_for_expression_use() -> u32 { + let x = 9; + + let _ = if x == 7 { + let _ = 19; + + let _splitter = 6; + + x << 2 + } else { + let _ = 19; + + x << 2 + }; + + if x == 9 { + let _ = 17; + + let _splitter = 6; + + x * 4 + } else { + let _ = 17; + + x * 4 + } +} + +fn main() {} diff --git a/tests/ui/branches_sharing_code/shared_at_top_and_bottom.stderr b/tests/ui/branches_sharing_code/shared_at_top_and_bottom.stderr new file mode 100644 index 00000000000..212cfb2f1d1 --- /dev/null +++ b/tests/ui/branches_sharing_code/shared_at_top_and_bottom.stderr @@ -0,0 +1,154 @@ +error: all if blocks contain the same code at the start and the end. Here at the start + --> $DIR/shared_at_top_and_bottom.rs:16:5 + | +LL | / if x == 7 { +LL | | let t = 7; +LL | | let _overlap_start = t * 2; +LL | | let _overlap_end = 2 * t; + | |_________________________________^ + | +note: the lint level is defined here + --> $DIR/shared_at_top_and_bottom.rs:2:36 + | +LL | #![deny(clippy::if_same_then_else, clippy::branches_sharing_code)] + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ +note: and here at the end + --> $DIR/shared_at_top_and_bottom.rs:28:5 + | +LL | / let _u = 9; +LL | | } + | |_____^ +help: consider moving the start statements out like this + | +LL | let t = 7; +LL | let _overlap_start = t * 2; +LL | let _overlap_end = 2 * t; +LL | if x == 7 { + | +help: and consider moving the end statements out like this + | +LL | } +LL | let _u = 9; + | + +error: all if blocks contain the same code at the start and the end. Here at the start + --> $DIR/shared_at_top_and_bottom.rs:32:5 + | +LL | / if x == 99 { +LL | | let r = 7; +LL | | let _overlap_start = r; +LL | | let _overlap_middle = r * r; + | |____________________________________^ + | +note: and here at the end + --> $DIR/shared_at_top_and_bottom.rs:43:5 + | +LL | / let _overlap_end = r * r * r; +LL | | let z = "end"; +LL | | } + | |_____^ + = warning: Some moved values might need to be renamed to avoid wrong references +help: consider moving the start statements out like this + | +LL | let r = 7; +LL | let _overlap_start = r; +LL | let _overlap_middle = r * r; +LL | if x == 99 { + | +help: and consider moving the end statements out like this + | +LL | } +LL | let _overlap_end = r * r * r; +LL | let z = "end"; + | + +error: all if blocks contain the same code at the start and the end. Here at the start + --> $DIR/shared_at_top_and_bottom.rs:61:5 + | +LL | / if (x > 7 && y < 13) || (x + y) % 2 == 1 { +LL | | let a = 0xcafe; +LL | | let b = 0xffff00ff; +LL | | let e_id = gen_id(a, b); + | |________________________________^ + | +note: and here at the end + --> $DIR/shared_at_top_and_bottom.rs:81:5 + | +LL | / let pack = DataPack { +LL | | id: e_id, +LL | | name: "Player 1".to_string(), +LL | | some_data: vec![0x12, 0x34, 0x56, 0x78, 0x90], +LL | | }; +LL | | process_data(pack); +LL | | } + | |_____^ + = warning: Some moved values might need to be renamed to avoid wrong references +help: consider moving the start statements out like this + | +LL | let a = 0xcafe; +LL | let b = 0xffff00ff; +LL | let e_id = gen_id(a, b); +LL | if (x > 7 && y < 13) || (x + y) % 2 == 1 { + | +help: and consider moving the end statements out like this + | +LL | } +LL | let pack = DataPack { +LL | id: e_id, +LL | name: "Player 1".to_string(), +LL | some_data: vec![0x12, 0x34, 0x56, 0x78, 0x90], +LL | }; + ... + +error: all if blocks contain the same code at the start and the end. Here at the start + --> $DIR/shared_at_top_and_bottom.rs:94:5 + | +LL | / let _ = if x == 7 { +LL | | let _ = 19; + | |___________________^ + | +note: and here at the end + --> $DIR/shared_at_top_and_bottom.rs:103:5 + | +LL | / x << 2 +LL | | }; + | |_____^ + = note: The end suggestion probably needs some adjustments to use the expression result correctly +help: consider moving the start statements out like this + | +LL | let _ = 19; +LL | let _ = if x == 7 { + | +help: and consider moving the end statements out like this + | +LL | } +LL | x << 2; + | + +error: all if blocks contain the same code at the start and the end. Here at the start + --> $DIR/shared_at_top_and_bottom.rs:106:5 + | +LL | / if x == 9 { +LL | | let _ = 17; + | |___________________^ + | +note: and here at the end + --> $DIR/shared_at_top_and_bottom.rs:115:5 + | +LL | / x * 4 +LL | | } + | |_____^ + = note: The end suggestion probably needs some adjustments to use the expression result correctly +help: consider moving the start statements out like this + | +LL | let _ = 17; +LL | if x == 9 { + | +help: and consider moving the end statements out like this + | +LL | } +LL | x * 4 + | + +error: aborting due to 5 previous errors + diff --git a/tests/ui/branches_sharing_code/valid_if_blocks.rs b/tests/ui/branches_sharing_code/valid_if_blocks.rs new file mode 100644 index 00000000000..0c70e3748ec --- /dev/null +++ b/tests/ui/branches_sharing_code/valid_if_blocks.rs @@ -0,0 +1,155 @@ +#![allow(dead_code, clippy::eval_order_dependence)] +#![deny(clippy::if_same_then_else, clippy::branches_sharing_code)] + +// This tests valid if blocks that shouldn't trigger the lint + +// Tests with value references are includes in "shared_code_at_bottom.rs" + +fn valid_examples() { + let x = 2; + + // The edge statements are different + if x == 9 { + let y = 1 << 5; + + println!("This is the same: vvv"); + let _z = y; + println!("The block expression is different"); + + println!("Different end 1"); + } else { + let y = 1 << 7; + + println!("This is the same: vvv"); + let _z = y; + println!("The block expression is different"); + + println!("Different end 2"); + } + + // No else + if x == 2 { + println!("Hello world!"); + println!("Hello back, how are you?"); + + // This is different vvvv + println!("Howdy stranger =^.^="); + + println!("Bye Bye World"); + } else if x == 9 { + println!("Hello world!"); + println!("Hello back, how are you?"); + + // This is different vvvv + println!("Hello reviewer :D"); + + println!("Bye Bye World"); + } + + // Overlapping statements only in else if blocks -> Don't lint + if x == 0 { + println!("I'm important!") + } else if x == 17 { + println!("I share code in else if"); + + println!("x is 17"); + } else { + println!("I share code in else if"); + + println!("x is nether x nor 17"); + } + + // Mutability is different + if x == 13 { + let mut y = 9; + println!("Value y is: {}", y); + y += 16; + let _z1 = y; + } else { + let y = 9; + println!("Value y is: {}", y); + let _z2 = y; + } + + // Same blocks but at start and bottom so no `if_same_then_else` lint + if x == 418 { + let y = 9; + let z = 8; + let _ = (x, y, z); + // Don't tell the programmer, my code is also in the else block + } else if x == 419 { + println!("+-----------+"); + println!("| |"); + println!("| O O |"); + println!("| ° |"); + println!("| \\_____/ |"); + println!("| |"); + println!("+-----------+"); + } else { + let y = 9; + let z = 8; + let _ = (x, y, z); + // I'm so much better than the x == 418 block. Trust me + } + + let x = 1; + if true { + println!("{}", x); + } else { + let x = 2; + println!("{}", x); + } + + // Let's test empty blocks + if false { + } else { + } +} + +/// This makes sure that the `if_same_then_else` masks the `shared_code_in_if_blocks` lint +fn trigger_other_lint() { + let x = 0; + let y = 1; + + // Same block + if x == 0 { + let u = 19; + println!("How are u today?"); + let _ = "This is a string"; + } else { + let u = 19; + println!("How are u today?"); + let _ = "This is a string"; + } + + // Only same expression + let _ = if x == 6 { 7 } else { 7 }; + + // Same in else if block + let _ = if x == 67 { + println!("Well I'm the most important block"); + "I'm a pretty string" + } else if x == 68 { + println!("I'm a doppelgänger"); + // Don't listen to my clone below + + if y == 90 { "=^.^=" } else { ":D" } + } else { + // Don't listen to my clone above + println!("I'm a doppelgänger"); + + if y == 90 { "=^.^=" } else { ":D" } + }; + + if x == 0 { + println!("I'm single"); + } else if x == 68 { + println!("I'm a doppelgänger"); + // Don't listen to my clone below + } else { + // Don't listen to my clone above + println!("I'm a doppelgänger"); + } +} + +fn main() {} diff --git a/tests/ui/branches_sharing_code/valid_if_blocks.stderr b/tests/ui/branches_sharing_code/valid_if_blocks.stderr new file mode 100644 index 00000000000..a815995e717 --- /dev/null +++ b/tests/ui/branches_sharing_code/valid_if_blocks.stderr @@ -0,0 +1,101 @@ +error: this `if` has identical blocks + --> $DIR/valid_if_blocks.rs:104:14 + | +LL | if false { + | ______________^ +LL | | } else { + | |_____^ + | +note: the lint level is defined here + --> $DIR/valid_if_blocks.rs:2:9 + | +LL | #![deny(clippy::if_same_then_else, clippy::branches_sharing_code)] + | ^^^^^^^^^^^^^^^^^^^^^^^^^ +note: same as this + --> $DIR/valid_if_blocks.rs:105:12 + | +LL | } else { + | ____________^ +LL | | } + | |_____^ + +error: this `if` has identical blocks + --> $DIR/valid_if_blocks.rs:115:15 + | +LL | if x == 0 { + | _______________^ +LL | | let u = 19; +LL | | println!("How are u today?"); +LL | | let _ = "This is a string"; +LL | | } else { + | |_____^ + | +note: same as this + --> $DIR/valid_if_blocks.rs:119:12 + | +LL | } else { + | ____________^ +LL | | let u = 19; +LL | | println!("How are u today?"); +LL | | let _ = "This is a string"; +LL | | } + | |_____^ + +error: this `if` has identical blocks + --> $DIR/valid_if_blocks.rs:126:23 + | +LL | let _ = if x == 6 { 7 } else { 7 }; + | ^^^^^ + | +note: same as this + --> $DIR/valid_if_blocks.rs:126:34 + | +LL | let _ = if x == 6 { 7 } else { 7 }; + | ^^^^^ + +error: this `if` has identical blocks + --> $DIR/valid_if_blocks.rs:132:23 + | +LL | } else if x == 68 { + | _______________________^ +LL | | println!("I'm a doppelgänger"); +LL | | // Don't listen to my clone below +LL | | +LL | | if y == 90 { "=^.^=" } else { ":D" } +LL | | } else { + | |_____^ + | +note: same as this + --> $DIR/valid_if_blocks.rs:137:12 + | +LL | } else { + | ____________^ +LL | | // Don't listen to my clone above +LL | | println!("I'm a doppelgänger"); +LL | | +LL | | if y == 90 { "=^.^=" } else { ":D" } +LL | | }; + | |_____^ + +error: this `if` has identical blocks + --> $DIR/valid_if_blocks.rs:146:23 + | +LL | } else if x == 68 { + | _______________________^ +LL | | println!("I'm a doppelgänger"); +LL | | // Don't listen to my clone below +LL | | } else { + | |_____^ + | +note: same as this + --> $DIR/valid_if_blocks.rs:149:12 + | +LL | } else { + | ____________^ +LL | | // Don't listen to my clone above +LL | | println!("I'm a doppelgänger"); +LL | | } + | |_____^ + +error: aborting due to 5 previous errors + diff --git a/tests/ui/checked_unwrap/complex_conditionals.rs b/tests/ui/checked_unwrap/complex_conditionals.rs index bb5b6f5ec04..ec082c73b44 100644 --- a/tests/ui/checked_unwrap/complex_conditionals.rs +++ b/tests/ui/checked_unwrap/complex_conditionals.rs @@ -1,5 +1,5 @@ #![deny(clippy::panicking_unwrap, clippy::unnecessary_unwrap)] -#![allow(clippy::if_same_then_else, clippy::shared_code_in_if_blocks)] +#![allow(clippy::if_same_then_else, clippy::branches_sharing_code)] fn test_complex_conditions() { let x: Result<(), ()> = Ok(()); diff --git a/tests/ui/checked_unwrap/complex_conditionals_nested.rs b/tests/ui/checked_unwrap/complex_conditionals_nested.rs index 99e8fb95466..043ea4148dc 100644 --- a/tests/ui/checked_unwrap/complex_conditionals_nested.rs +++ b/tests/ui/checked_unwrap/complex_conditionals_nested.rs @@ -1,5 +1,5 @@ #![deny(clippy::panicking_unwrap, clippy::unnecessary_unwrap)] -#![allow(clippy::if_same_then_else, clippy::shared_code_in_if_blocks)] +#![allow(clippy::if_same_then_else, clippy::branches_sharing_code)] fn test_nested() { fn nested() { diff --git a/tests/ui/checked_unwrap/simple_conditionals.rs b/tests/ui/checked_unwrap/simple_conditionals.rs index 0459100d88a..8f23fb28827 100644 --- a/tests/ui/checked_unwrap/simple_conditionals.rs +++ b/tests/ui/checked_unwrap/simple_conditionals.rs @@ -1,5 +1,5 @@ #![deny(clippy::panicking_unwrap, clippy::unnecessary_unwrap)] -#![allow(clippy::if_same_then_else, clippy::shared_code_in_if_blocks)] +#![allow(clippy::if_same_then_else, clippy::branches_sharing_code)] macro_rules! m { ($a:expr) => { diff --git a/tests/ui/default_numeric_fallback.rs b/tests/ui/default_numeric_fallback.rs index de89f806c58..43468872db0 100644 --- a/tests/ui/default_numeric_fallback.rs +++ b/tests/ui/default_numeric_fallback.rs @@ -3,7 +3,7 @@ #![allow(clippy::never_loop)] #![allow(clippy::no_effect)] #![allow(clippy::unnecessary_operation)] -#![allow(clippy::shared_code_in_if_blocks)] +#![allow(clippy::branches_sharing_code)] mod basic_expr { fn test() { diff --git a/tests/ui/if_same_then_else.rs b/tests/ui/if_same_then_else.rs index 35a2e139c11..ef956745500 100644 --- a/tests/ui/if_same_then_else.rs +++ b/tests/ui/if_same_then_else.rs @@ -6,7 +6,7 @@ clippy::no_effect, clippy::unused_unit, clippy::zero_divided_by_zero, - clippy::shared_code_in_if_blocks + clippy::branches_sharing_code )] struct Foo { diff --git a/tests/ui/if_same_then_else2.rs b/tests/ui/if_same_then_else2.rs index 8164b125570..e4dc5b647df 100644 --- a/tests/ui/if_same_then_else2.rs +++ b/tests/ui/if_same_then_else2.rs @@ -6,7 +6,7 @@ clippy::ifs_same_cond, clippy::needless_return, clippy::single_element_loop, - clippy::shared_code_in_if_blocks + clippy::branches_sharing_code )] fn if_same_then_else2() -> Result<&'static str, ()> { diff --git a/tests/ui/let_if_seq.rs b/tests/ui/let_if_seq.rs index 9fd3f875a5f..2d8f3c2f0e7 100644 --- a/tests/ui/let_if_seq.rs +++ b/tests/ui/let_if_seq.rs @@ -3,7 +3,7 @@ unused_assignments, clippy::similar_names, clippy::blacklisted_name, - clippy::shared_code_in_if_blocks + clippy::branches_sharing_code )] #![warn(clippy::useless_let_if_seq)] diff --git a/tests/ui/needless_bool/simple.rs b/tests/ui/needless_bool/simple.rs index 678040fa98b..588bb88f446 100644 --- a/tests/ui/needless_bool/simple.rs +++ b/tests/ui/needless_bool/simple.rs @@ -5,7 +5,7 @@ clippy::no_effect, clippy::if_same_then_else, clippy::needless_return, - clippy::shared_code_in_if_blocks + clippy::branches_sharing_code )] fn main() { diff --git a/tests/ui/needless_return.fixed b/tests/ui/needless_return.fixed index ebf74cfef12..82d95cc041f 100644 --- a/tests/ui/needless_return.fixed +++ b/tests/ui/needless_return.fixed @@ -4,7 +4,7 @@ #![allow( clippy::if_same_then_else, clippy::single_match, - clippy::shared_code_in_if_blocks, + clippy::branches_sharing_code, clippy::needless_bool )] #![warn(clippy::needless_return)] diff --git a/tests/ui/needless_return.rs b/tests/ui/needless_return.rs index 2bebccdabca..8a471f802e1 100644 --- a/tests/ui/needless_return.rs +++ b/tests/ui/needless_return.rs @@ -4,7 +4,7 @@ #![allow( clippy::if_same_then_else, clippy::single_match, - clippy::shared_code_in_if_blocks, + clippy::branches_sharing_code, clippy::needless_bool )] #![warn(clippy::needless_return)] diff --git a/tests/ui/shared_code_in_if_blocks/shared_at_bot.rs b/tests/ui/shared_code_in_if_blocks/shared_at_bot.rs deleted file mode 100644 index 6ff362cb752..00000000000 --- a/tests/ui/shared_code_in_if_blocks/shared_at_bot.rs +++ /dev/null @@ -1,209 +0,0 @@ -#![allow(dead_code)] -#![deny(clippy::if_same_then_else, clippy::shared_code_in_if_blocks)] - -// This tests the shared_code_in_if_blocks lint at the end of blocks - -fn simple_examples() { - let x = 1; - - let _ = if x == 7 { - println!("Branch I"); - let start_value = 0; - println!("=^.^="); - - // Same but not moveable due to `start_value` - let _ = start_value; - - // The rest is self contained and moveable => Only lint the rest - let result = false; - println!("Block end!"); - result - } else { - println!("Branch II"); - let start_value = 8; - println!("xD"); - - // Same but not moveable due to `start_value` - let _ = start_value; - - // The rest is self contained and moveable => Only lint the rest - let result = false; - println!("Block end!"); - result - }; - - // Else if block - if x == 9 { - println!("The index is: 6"); - - println!("Same end of block"); - } else if x == 8 { - println!("The index is: 4"); - - // We should only get a lint trigger for the last statement - println!("This is also eq with the else block"); - println!("Same end of block"); - } else { - println!("This is also eq with the else block"); - println!("Same end of block"); - } - - // Use of outer scope value - let outer_scope_value = "I'm outside the if block"; - if x < 99 { - let z = "How are you"; - println!("I'm a local because I use the value `z`: `{}`", z); - - println!( - "I'm moveable because I know: `outer_scope_value`: '{}'", - outer_scope_value - ); - } else { - let z = 45678000; - println!("I'm a local because I use the value `z`: `{}`", z); - - println!( - "I'm moveable because I know: `outer_scope_value`: '{}'", - outer_scope_value - ); - } - - if x == 9 { - if x == 8 { - // No parent!! - println!("---"); - println!("Hello World"); - } else { - println!("Hello World"); - } - } -} - -/// Simple examples where the move can cause some problems due to moved values -fn simple_but_suggestion_is_invalid() { - let x = 16; - - // Local value - let later_used_value = 17; - if x == 9 { - let _ = 9; - let later_used_value = "A string value"; - println!("{}", later_used_value); - } else { - let later_used_value = "A string value"; - println!("{}", later_used_value); - // I'm expecting a note about this - } - println!("{}", later_used_value); - - // outer function - if x == 78 { - let simple_examples = "I now identify as a &str :)"; - println!("This is the new simple_example: {}", simple_examples); - } else { - println!("Separator print statement"); - - let simple_examples = "I now identify as a &str :)"; - println!("This is the new simple_example: {}", simple_examples); - } - simple_examples(); -} - -/// Tests where the blocks are not linted due to the used value scope -fn not_moveable_due_to_value_scope() { - let x = 18; - - // Using a local value in the moved code - if x == 9 { - let y = 18; - println!("y is: `{}`", y); - } else { - let y = "A string"; - println!("y is: `{}`", y); - } - - // Using a local value in the expression - let _ = if x == 0 { - let mut result = x + 1; - - println!("1. Doing some calculations"); - println!("2. Some more calculations"); - println!("3. Setting result"); - - result - } else { - let mut result = x - 1; - - println!("1. Doing some calculations"); - println!("2. Some more calculations"); - println!("3. Setting result"); - - result - }; - - let _ = if x == 7 { - let z1 = 100; - println!("z1: {}", z1); - - let z2 = z1; - println!("z2: {}", z2); - - z2 - } else { - let z1 = 300; - println!("z1: {}", z1); - - let z2 = z1; - println!("z2: {}", z2); - - z2 - }; -} - -/// This should add a note to the lint msg since the moved expression is not `()` -fn added_note_for_expression_use() -> u32 { - let x = 9; - - let _ = if x == 7 { - x << 2 - } else { - let _ = 6; - x << 2 - }; - - if x == 9 { - x * 4 - } else { - let _ = 17; - x * 4 - } -} - -#[rustfmt::skip] -fn test_suggestion_with_weird_formatting() { - let x = 9; - let mut a = 0; - let mut b = 0; - - // The error message still looks weird tbh but this is the best I can do - // for weird formatting - if x == 17 { b = 1; a = 0x99; } else { a = 0x99; } -} - -fn fp_test() { - let x = 17; - - if x == 18 { - let y = 19; - if y < x { - println!("Trigger") - } - } else { - let z = 166; - if z < x { - println!("Trigger") - } - } -} - -fn main() {} diff --git a/tests/ui/shared_code_in_if_blocks/shared_at_bot.stderr b/tests/ui/shared_code_in_if_blocks/shared_at_bot.stderr deleted file mode 100644 index 2268d8f5365..00000000000 --- a/tests/ui/shared_code_in_if_blocks/shared_at_bot.stderr +++ /dev/null @@ -1,143 +0,0 @@ -error: all if blocks contain the same code at the end - --> $DIR/shared_at_bot.rs:30:5 - | -LL | / let result = false; -LL | | println!("Block end!"); -LL | | result -LL | | }; - | |_____^ - | -note: the lint level is defined here - --> $DIR/shared_at_bot.rs:2:36 - | -LL | #![deny(clippy::if_same_then_else, clippy::shared_code_in_if_blocks)] - | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ - = note: The end suggestion probably needs some adjustments to use the expression result correctly -help: consider moving the end statements out like this - | -LL | } -LL | let result = false; -LL | println!("Block end!"); -LL | result; - | - -error: all if blocks contain the same code at the end - --> $DIR/shared_at_bot.rs:48:5 - | -LL | / println!("Same end of block"); -LL | | } - | |_____^ - | -help: consider moving the end statements out like this - | -LL | } -LL | println!("Same end of block"); - | - -error: all if blocks contain the same code at the end - --> $DIR/shared_at_bot.rs:65:5 - | -LL | / println!( -LL | | "I'm moveable because I know: `outer_scope_value`: '{}'", -LL | | outer_scope_value -LL | | ); -LL | | } - | |_____^ - | -help: consider moving the end statements out like this - | -LL | } -LL | println!( -LL | "I'm moveable because I know: `outer_scope_value`: '{}'", -LL | outer_scope_value -LL | ); - | - -error: all if blocks contain the same code at the end - --> $DIR/shared_at_bot.rs:77:9 - | -LL | / println!("Hello World"); -LL | | } - | |_________^ - | -help: consider moving the end statements out like this - | -LL | } -LL | println!("Hello World"); - | - -error: all if blocks contain the same code at the end - --> $DIR/shared_at_bot.rs:93:5 - | -LL | / let later_used_value = "A string value"; -LL | | println!("{}", later_used_value); -LL | | // I'm expecting a note about this -LL | | } - | |_____^ - | - = warning: Some moved values might need to be renamed to avoid wrong references -help: consider moving the end statements out like this - | -LL | } -LL | let later_used_value = "A string value"; -LL | println!("{}", later_used_value); - | - -error: all if blocks contain the same code at the end - --> $DIR/shared_at_bot.rs:106:5 - | -LL | / let simple_examples = "I now identify as a &str :)"; -LL | | println!("This is the new simple_example: {}", simple_examples); -LL | | } - | |_____^ - | - = warning: Some moved values might need to be renamed to avoid wrong references -help: consider moving the end statements out like this - | -LL | } -LL | let simple_examples = "I now identify as a &str :)"; -LL | println!("This is the new simple_example: {}", simple_examples); - | - -error: all if blocks contain the same code at the end - --> $DIR/shared_at_bot.rs:171:5 - | -LL | / x << 2 -LL | | }; - | |_____^ - | - = note: The end suggestion probably needs some adjustments to use the expression result correctly -help: consider moving the end statements out like this - | -LL | } -LL | x << 2; - | - -error: all if blocks contain the same code at the end - --> $DIR/shared_at_bot.rs:178:5 - | -LL | / x * 4 -LL | | } - | |_____^ - | - = note: The end suggestion probably needs some adjustments to use the expression result correctly -help: consider moving the end statements out like this - | -LL | } -LL | x * 4 - | - -error: all if blocks contain the same code at the end - --> $DIR/shared_at_bot.rs:190:44 - | -LL | if x == 17 { b = 1; a = 0x99; } else { a = 0x99; } - | ^^^^^^^^^^^ - | -help: consider moving the end statements out like this - | -LL | if x == 17 { b = 1; a = 0x99; } else { } -LL | a = 0x99; - | - -error: aborting due to 9 previous errors - diff --git a/tests/ui/shared_code_in_if_blocks/shared_at_top.rs b/tests/ui/shared_code_in_if_blocks/shared_at_top.rs deleted file mode 100644 index 496939f2a5e..00000000000 --- a/tests/ui/shared_code_in_if_blocks/shared_at_top.rs +++ /dev/null @@ -1,103 +0,0 @@ -#![allow(dead_code, clippy::eval_order_dependence)] -#![deny(clippy::if_same_then_else, clippy::shared_code_in_if_blocks)] - -// This tests the shared_code_in_if_blocks lint at the start of blocks - -fn simple_examples() { - let x = 0; - - // Simple - if true { - println!("Hello World!"); - println!("I'm branch nr: 1"); - } else { - println!("Hello World!"); - println!("I'm branch nr: 2"); - } - - // Else if - if x == 0 { - let y = 9; - println!("The value y was set to: `{}`", y); - let _z = y; - - println!("I'm the true start index of arrays"); - } else if x == 1 { - let y = 9; - println!("The value y was set to: `{}`", y); - let _z = y; - - println!("I start counting from 1 so my array starts from `1`"); - } else { - let y = 9; - println!("The value y was set to: `{}`", y); - let _z = y; - - println!("Ha, Pascal allows you to start the array where you want") - } - - // Return a value - let _ = if x == 7 { - let y = 16; - println!("What can I say except: \"you're welcome?\""); - let _ = y; - x - } else { - let y = 16; - println!("Thank you"); - y - }; -} - -/// Simple examples where the move can cause some problems due to moved values -fn simple_but_suggestion_is_invalid() { - let x = 10; - - // Can't be automatically moved because used_value_name is getting used again - let used_value_name = 19; - if x == 10 { - let used_value_name = "Different type"; - println!("Str: {}", used_value_name); - let _ = 1; - } else { - let used_value_name = "Different type"; - println!("Str: {}", used_value_name); - let _ = 2; - } - let _ = used_value_name; - - // This can be automatically moved as `can_be_overridden` is not used again - let can_be_overridden = 8; - let _ = can_be_overridden; - if x == 11 { - let can_be_overridden = "Move me"; - println!("I'm also moveable"); - let _ = 111; - } else { - let can_be_overridden = "Move me"; - println!("I'm also moveable"); - let _ = 222; - } -} - -/// This function tests that the `IS_SAME_THAN_ELSE` only covers the lint if it's enabled. -fn check_if_same_than_else_mask() { - let x = 2021; - - #[allow(clippy::if_same_then_else)] - if x == 2020 { - println!("This should trigger the `SHARED_CODE_IN_IF_BLOCKS` lint."); - println!("Because `IF_SAME_THEN_ELSE` is allowed here"); - } else { - println!("This should trigger the `SHARED_CODE_IN_IF_BLOCKS` lint."); - println!("Because `IF_SAME_THEN_ELSE` is allowed here"); - } - - if x == 2019 { - println!("This should trigger `IS_SAME_THAN_ELSE` as usual"); - } else { - println!("This should trigger `IS_SAME_THAN_ELSE` as usual"); - } -} - -fn main() {} diff --git a/tests/ui/shared_code_in_if_blocks/shared_at_top.stderr b/tests/ui/shared_code_in_if_blocks/shared_at_top.stderr deleted file mode 100644 index 76898a6ff02..00000000000 --- a/tests/ui/shared_code_in_if_blocks/shared_at_top.stderr +++ /dev/null @@ -1,121 +0,0 @@ -error: all if blocks contain the same code at the start - --> $DIR/shared_at_top.rs:10:5 - | -LL | / if true { -LL | | println!("Hello World!"); - | |_________________________________^ - | -note: the lint level is defined here - --> $DIR/shared_at_top.rs:2:36 - | -LL | #![deny(clippy::if_same_then_else, clippy::shared_code_in_if_blocks)] - | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ -help: consider moving the start statements out like this - | -LL | println!("Hello World!"); -LL | if true { - | - -error: all if blocks contain the same code at the start - --> $DIR/shared_at_top.rs:19:5 - | -LL | / if x == 0 { -LL | | let y = 9; -LL | | println!("The value y was set to: `{}`", y); -LL | | let _z = y; - | |___________________^ - | - = warning: Some moved values might need to be renamed to avoid wrong references -help: consider moving the start statements out like this - | -LL | let y = 9; -LL | println!("The value y was set to: `{}`", y); -LL | let _z = y; -LL | if x == 0 { - | - -error: all if blocks contain the same code at the start - --> $DIR/shared_at_top.rs:40:5 - | -LL | / let _ = if x == 7 { -LL | | let y = 16; - | |___________________^ - | -help: consider moving the start statements out like this - | -LL | let y = 16; -LL | let _ = if x == 7 { - | - -error: all if blocks contain the same code at the start - --> $DIR/shared_at_top.rs:58:5 - | -LL | / if x == 10 { -LL | | let used_value_name = "Different type"; -LL | | println!("Str: {}", used_value_name); - | |_____________________________________________^ - | - = warning: Some moved values might need to be renamed to avoid wrong references -help: consider moving the start statements out like this - | -LL | let used_value_name = "Different type"; -LL | println!("Str: {}", used_value_name); -LL | if x == 10 { - | - -error: all if blocks contain the same code at the start - --> $DIR/shared_at_top.rs:72:5 - | -LL | / if x == 11 { -LL | | let can_be_overridden = "Move me"; -LL | | println!("I'm also moveable"); - | |______________________________________^ - | - = warning: Some moved values might need to be renamed to avoid wrong references -help: consider moving the start statements out like this - | -LL | let can_be_overridden = "Move me"; -LL | println!("I'm also moveable"); -LL | if x == 11 { - | - -error: all if blocks contain the same code at the start - --> $DIR/shared_at_top.rs:88:5 - | -LL | / if x == 2020 { -LL | | println!("This should trigger the `SHARED_CODE_IN_IF_BLOCKS` lint."); -LL | | println!("Because `IF_SAME_THEN_ELSE` is allowed here"); - | |________________________________________________________________^ - | -help: consider moving the start statements out like this - | -LL | println!("This should trigger the `SHARED_CODE_IN_IF_BLOCKS` lint."); -LL | println!("Because `IF_SAME_THEN_ELSE` is allowed here"); -LL | if x == 2020 { - | - -error: this `if` has identical blocks - --> $DIR/shared_at_top.rs:96:18 - | -LL | if x == 2019 { - | __________________^ -LL | | println!("This should trigger `IS_SAME_THAN_ELSE` as usual"); -LL | | } else { - | |_____^ - | -note: the lint level is defined here - --> $DIR/shared_at_top.rs:2:9 - | -LL | #![deny(clippy::if_same_then_else, clippy::shared_code_in_if_blocks)] - | ^^^^^^^^^^^^^^^^^^^^^^^^^ -note: same as this - --> $DIR/shared_at_top.rs:98:12 - | -LL | } else { - | ____________^ -LL | | println!("This should trigger `IS_SAME_THAN_ELSE` as usual"); -LL | | } - | |_____^ - -error: aborting due to 7 previous errors - diff --git a/tests/ui/shared_code_in_if_blocks/shared_at_top_and_bot.rs b/tests/ui/shared_code_in_if_blocks/shared_at_top_and_bot.rs deleted file mode 100644 index 46a8f931aaf..00000000000 --- a/tests/ui/shared_code_in_if_blocks/shared_at_top_and_bot.rs +++ /dev/null @@ -1,119 +0,0 @@ -#![allow(dead_code)] -#![deny(clippy::if_same_then_else, clippy::shared_code_in_if_blocks)] - -// shared_code_in_if_blocks at the top and bottom of the if blocks - -struct DataPack { - id: u32, - name: String, - some_data: Vec, -} - -fn overlapping_eq_regions() { - let x = 9; - - // Overlap with separator - if x == 7 { - let t = 7; - let _overlap_start = t * 2; - let _overlap_end = 2 * t; - let _u = 9; - } else { - let t = 7; - let _overlap_start = t * 2; - let _overlap_end = 2 * t; - println!("Overlap separator"); - let _overlap_start = t * 2; - let _overlap_end = 2 * t; - let _u = 9; - } - - // Overlap with separator - if x == 99 { - let r = 7; - let _overlap_start = r; - let _overlap_middle = r * r; - let _overlap_end = r * r * r; - let z = "end"; - } else { - let r = 7; - let _overlap_start = r; - let _overlap_middle = r * r; - let _overlap_middle = r * r; - let _overlap_end = r * r * r; - let z = "end"; - } -} - -fn complexer_example() { - fn gen_id(x: u32, y: u32) -> u32 { - let x = x & 0x0000_ffff; - let y = (y & 0xffff_0000) << 16; - x | y - } - - fn process_data(data: DataPack) { - let _ = data; - } - - let x = 8; - let y = 9; - if (x > 7 && y < 13) || (x + y) % 2 == 1 { - let a = 0xcafe; - let b = 0xffff00ff; - let e_id = gen_id(a, b); - - println!("From the a `{}` to the b `{}`", a, b); - - let pack = DataPack { - id: e_id, - name: "Player 1".to_string(), - some_data: vec![0x12, 0x34, 0x56, 0x78, 0x90], - }; - process_data(pack); - } else { - let a = 0xcafe; - let b = 0xffff00ff; - let e_id = gen_id(a, b); - - println!("The new ID is '{}'", e_id); - - let pack = DataPack { - id: e_id, - name: "Player 1".to_string(), - some_data: vec![0x12, 0x34, 0x56, 0x78, 0x90], - }; - process_data(pack); - } -} - -/// This should add a note to the lint msg since the moved expression is not `()` -fn added_note_for_expression_use() -> u32 { - let x = 9; - - let _ = if x == 7 { - let _ = 19; - - let _splitter = 6; - - x << 2 - } else { - let _ = 19; - - x << 2 - }; - - if x == 9 { - let _ = 17; - - let _splitter = 6; - - x * 4 - } else { - let _ = 17; - - x * 4 - } -} - -fn main() {} diff --git a/tests/ui/shared_code_in_if_blocks/shared_at_top_and_bot.stderr b/tests/ui/shared_code_in_if_blocks/shared_at_top_and_bot.stderr deleted file mode 100644 index 75c3d397f2e..00000000000 --- a/tests/ui/shared_code_in_if_blocks/shared_at_top_and_bot.stderr +++ /dev/null @@ -1,154 +0,0 @@ -error: all if blocks contain the same code at the start and the end. Here at the start - --> $DIR/shared_at_top_and_bot.rs:16:5 - | -LL | / if x == 7 { -LL | | let t = 7; -LL | | let _overlap_start = t * 2; -LL | | let _overlap_end = 2 * t; - | |_________________________________^ - | -note: the lint level is defined here - --> $DIR/shared_at_top_and_bot.rs:2:36 - | -LL | #![deny(clippy::if_same_then_else, clippy::shared_code_in_if_blocks)] - | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ -note: and here at the end - --> $DIR/shared_at_top_and_bot.rs:28:5 - | -LL | / let _u = 9; -LL | | } - | |_____^ -help: consider moving the start statements out like this - | -LL | let t = 7; -LL | let _overlap_start = t * 2; -LL | let _overlap_end = 2 * t; -LL | if x == 7 { - | -help: and consider moving the end statements out like this - | -LL | } -LL | let _u = 9; - | - -error: all if blocks contain the same code at the start and the end. Here at the start - --> $DIR/shared_at_top_and_bot.rs:32:5 - | -LL | / if x == 99 { -LL | | let r = 7; -LL | | let _overlap_start = r; -LL | | let _overlap_middle = r * r; - | |____________________________________^ - | -note: and here at the end - --> $DIR/shared_at_top_and_bot.rs:43:5 - | -LL | / let _overlap_end = r * r * r; -LL | | let z = "end"; -LL | | } - | |_____^ - = warning: Some moved values might need to be renamed to avoid wrong references -help: consider moving the start statements out like this - | -LL | let r = 7; -LL | let _overlap_start = r; -LL | let _overlap_middle = r * r; -LL | if x == 99 { - | -help: and consider moving the end statements out like this - | -LL | } -LL | let _overlap_end = r * r * r; -LL | let z = "end"; - | - -error: all if blocks contain the same code at the start and the end. Here at the start - --> $DIR/shared_at_top_and_bot.rs:61:5 - | -LL | / if (x > 7 && y < 13) || (x + y) % 2 == 1 { -LL | | let a = 0xcafe; -LL | | let b = 0xffff00ff; -LL | | let e_id = gen_id(a, b); - | |________________________________^ - | -note: and here at the end - --> $DIR/shared_at_top_and_bot.rs:81:5 - | -LL | / let pack = DataPack { -LL | | id: e_id, -LL | | name: "Player 1".to_string(), -LL | | some_data: vec![0x12, 0x34, 0x56, 0x78, 0x90], -LL | | }; -LL | | process_data(pack); -LL | | } - | |_____^ - = warning: Some moved values might need to be renamed to avoid wrong references -help: consider moving the start statements out like this - | -LL | let a = 0xcafe; -LL | let b = 0xffff00ff; -LL | let e_id = gen_id(a, b); -LL | if (x > 7 && y < 13) || (x + y) % 2 == 1 { - | -help: and consider moving the end statements out like this - | -LL | } -LL | let pack = DataPack { -LL | id: e_id, -LL | name: "Player 1".to_string(), -LL | some_data: vec![0x12, 0x34, 0x56, 0x78, 0x90], -LL | }; - ... - -error: all if blocks contain the same code at the start and the end. Here at the start - --> $DIR/shared_at_top_and_bot.rs:94:5 - | -LL | / let _ = if x == 7 { -LL | | let _ = 19; - | |___________________^ - | -note: and here at the end - --> $DIR/shared_at_top_and_bot.rs:103:5 - | -LL | / x << 2 -LL | | }; - | |_____^ - = note: The end suggestion probably needs some adjustments to use the expression result correctly -help: consider moving the start statements out like this - | -LL | let _ = 19; -LL | let _ = if x == 7 { - | -help: and consider moving the end statements out like this - | -LL | } -LL | x << 2; - | - -error: all if blocks contain the same code at the start and the end. Here at the start - --> $DIR/shared_at_top_and_bot.rs:106:5 - | -LL | / if x == 9 { -LL | | let _ = 17; - | |___________________^ - | -note: and here at the end - --> $DIR/shared_at_top_and_bot.rs:115:5 - | -LL | / x * 4 -LL | | } - | |_____^ - = note: The end suggestion probably needs some adjustments to use the expression result correctly -help: consider moving the start statements out like this - | -LL | let _ = 17; -LL | if x == 9 { - | -help: and consider moving the end statements out like this - | -LL | } -LL | x * 4 - | - -error: aborting due to 5 previous errors - diff --git a/tests/ui/shared_code_in_if_blocks/valid_if_blocks.rs b/tests/ui/shared_code_in_if_blocks/valid_if_blocks.rs deleted file mode 100644 index e63490d5b07..00000000000 --- a/tests/ui/shared_code_in_if_blocks/valid_if_blocks.rs +++ /dev/null @@ -1,155 +0,0 @@ -#![allow(dead_code, clippy::eval_order_dependence)] -#![deny(clippy::if_same_then_else, clippy::shared_code_in_if_blocks)] - -// This tests the shared_code_in_if_blocks lint at the start of blocks - -// Tests with value references are includes in "shared_code_at_bot.rs" - -fn valid_examples() { - let x = 2; - - // The edge statements are different - if x == 9 { - let y = 1 << 5; - - println!("This is the same: vvv"); - let _z = y; - println!("The block expression is different"); - - println!("Different end 1"); - } else { - let y = 1 << 7; - - println!("This is the same: vvv"); - let _z = y; - println!("The block expression is different"); - - println!("Different end 2"); - } - - // No else - if x == 2 { - println!("Hello world!"); - println!("Hello back, how are you?"); - - // This is different vvvv - println!("Howdy stranger =^.^="); - - println!("Bye Bye World"); - } else if x == 9 { - println!("Hello world!"); - println!("Hello back, how are you?"); - - // This is different vvvv - println!("Hello reviewer :D"); - - println!("Bye Bye World"); - } - - // Overlapping statements only in else if blocks -> Don't lint - if x == 0 { - println!("I'm important!") - } else if x == 17 { - println!("I share code in else if"); - - println!("x is 17"); - } else { - println!("I share code in else if"); - - println!("x is nether x nor 17"); - } - - // Mutability is different - if x == 13 { - let mut y = 9; - println!("Value y is: {}", y); - y += 16; - let _z1 = y; - } else { - let y = 9; - println!("Value y is: {}", y); - let _z2 = y; - } - - // Same blocks but at start and bottom so no `if_same_then_else` lint - if x == 418 { - let y = 9; - let z = 8; - let _ = (x, y, z); - // Don't tell the programmer, my code is also in the else block - } else if x == 419 { - println!("+-----------+"); - println!("| |"); - println!("| O O |"); - println!("| ° |"); - println!("| \\_____/ |"); - println!("| |"); - println!("+-----------+"); - } else { - let y = 9; - let z = 8; - let _ = (x, y, z); - // I'm so much better than the x == 418 block. Trust me - } - - let x = 1; - if true { - println!("{}", x); - } else { - let x = 2; - println!("{}", x); - } - - // Let's test empty blocks - if false { - } else { - } -} - -/// This makes sure that the `if_same_then_else` masks the `shared_code_in_if_blocks` lint -fn trigger_other_lint() { - let x = 0; - let y = 1; - - // Same block - if x == 0 { - let u = 19; - println!("How are u today?"); - let _ = "This is a string"; - } else { - let u = 19; - println!("How are u today?"); - let _ = "This is a string"; - } - - // Only same expression - let _ = if x == 6 { 7 } else { 7 }; - - // Same in else if block - let _ = if x == 67 { - println!("Well I'm the most important block"); - "I'm a pretty string" - } else if x == 68 { - println!("I'm a doppelgänger"); - // Don't listen to my clone below - - if y == 90 { "=^.^=" } else { ":D" } - } else { - // Don't listen to my clone above - println!("I'm a doppelgänger"); - - if y == 90 { "=^.^=" } else { ":D" } - }; - - if x == 0 { - println!("I'm single"); - } else if x == 68 { - println!("I'm a doppelgänger"); - // Don't listen to my clone below - } else { - // Don't listen to my clone above - println!("I'm a doppelgänger"); - } -} - -fn main() {} diff --git a/tests/ui/shared_code_in_if_blocks/valid_if_blocks.stderr b/tests/ui/shared_code_in_if_blocks/valid_if_blocks.stderr deleted file mode 100644 index 846581456dc..00000000000 --- a/tests/ui/shared_code_in_if_blocks/valid_if_blocks.stderr +++ /dev/null @@ -1,101 +0,0 @@ -error: this `if` has identical blocks - --> $DIR/valid_if_blocks.rs:104:14 - | -LL | if false { - | ______________^ -LL | | } else { - | |_____^ - | -note: the lint level is defined here - --> $DIR/valid_if_blocks.rs:2:9 - | -LL | #![deny(clippy::if_same_then_else, clippy::shared_code_in_if_blocks)] - | ^^^^^^^^^^^^^^^^^^^^^^^^^ -note: same as this - --> $DIR/valid_if_blocks.rs:105:12 - | -LL | } else { - | ____________^ -LL | | } - | |_____^ - -error: this `if` has identical blocks - --> $DIR/valid_if_blocks.rs:115:15 - | -LL | if x == 0 { - | _______________^ -LL | | let u = 19; -LL | | println!("How are u today?"); -LL | | let _ = "This is a string"; -LL | | } else { - | |_____^ - | -note: same as this - --> $DIR/valid_if_blocks.rs:119:12 - | -LL | } else { - | ____________^ -LL | | let u = 19; -LL | | println!("How are u today?"); -LL | | let _ = "This is a string"; -LL | | } - | |_____^ - -error: this `if` has identical blocks - --> $DIR/valid_if_blocks.rs:126:23 - | -LL | let _ = if x == 6 { 7 } else { 7 }; - | ^^^^^ - | -note: same as this - --> $DIR/valid_if_blocks.rs:126:34 - | -LL | let _ = if x == 6 { 7 } else { 7 }; - | ^^^^^ - -error: this `if` has identical blocks - --> $DIR/valid_if_blocks.rs:132:23 - | -LL | } else if x == 68 { - | _______________________^ -LL | | println!("I'm a doppelgänger"); -LL | | // Don't listen to my clone below -LL | | -LL | | if y == 90 { "=^.^=" } else { ":D" } -LL | | } else { - | |_____^ - | -note: same as this - --> $DIR/valid_if_blocks.rs:137:12 - | -LL | } else { - | ____________^ -LL | | // Don't listen to my clone above -LL | | println!("I'm a doppelgänger"); -LL | | -LL | | if y == 90 { "=^.^=" } else { ":D" } -LL | | }; - | |_____^ - -error: this `if` has identical blocks - --> $DIR/valid_if_blocks.rs:146:23 - | -LL | } else if x == 68 { - | _______________________^ -LL | | println!("I'm a doppelgänger"); -LL | | // Don't listen to my clone below -LL | | } else { - | |_____^ - | -note: same as this - --> $DIR/valid_if_blocks.rs:149:12 - | -LL | } else { - | ____________^ -LL | | // Don't listen to my clone above -LL | | println!("I'm a doppelgänger"); -LL | | } - | |_____^ - -error: aborting due to 5 previous errors - -- cgit 1.4.1-3-g733a5