diff options
Diffstat (limited to 'tests/ui/branches_sharing_code')
8 files changed, 97 insertions, 36 deletions
diff --git a/tests/ui/branches_sharing_code/shared_at_bottom.rs b/tests/ui/branches_sharing_code/shared_at_bottom.rs index 6a63008b5a7..d102efa7a58 100644 --- a/tests/ui/branches_sharing_code/shared_at_bottom.rs +++ b/tests/ui/branches_sharing_code/shared_at_bottom.rs @@ -1,7 +1,7 @@ #![deny(clippy::if_same_then_else, clippy::branches_sharing_code)] #![allow(dead_code)] #![allow(clippy::equatable_if_let, clippy::uninlined_format_args)] - +//@no-rustfix // This tests the branches_sharing_code lint at the end of blocks fn simple_examples() { @@ -29,6 +29,8 @@ fn simple_examples() { // The rest is self contained and moveable => Only lint the rest let result = false; + //~^ ERROR: all if blocks contain the same code at the end + //~| NOTE: the end suggestion probably needs some adjustments to use the expressio println!("Block end!"); result }; @@ -47,6 +49,7 @@ fn simple_examples() { } else { println!("This is also eq with the else block"); println!("Same end of block"); + //~^ ERROR: all if blocks contain the same code at the end } // Use of outer scope value @@ -64,6 +67,7 @@ fn simple_examples() { println!("I'm a local because I use the value `z`: `{}`", z); println!( + //~^ ERROR: all if blocks contain the same code at the end "I'm moveable because I know: `outer_scope_value`: '{}'", outer_scope_value ); @@ -76,6 +80,7 @@ fn simple_examples() { println!("Hello World"); } else { println!("Hello World"); + //~^ ERROR: all if blocks contain the same code at the end } } } @@ -92,6 +97,7 @@ fn simple_but_suggestion_is_invalid() { println!("{}", later_used_value); } else { let later_used_value = "A string value"; + //~^ ERROR: all if blocks contain the same code at the end println!("{}", later_used_value); // I'm expecting a note about this } @@ -105,6 +111,7 @@ fn simple_but_suggestion_is_invalid() { println!("Separator print statement"); let simple_examples = "I now identify as a &str :)"; + //~^ ERROR: all if blocks contain the same code at the end println!("This is the new simple_example: {}", simple_examples); } simple_examples(); @@ -170,6 +177,8 @@ fn added_note_for_expression_use() -> u32 { } else { let _ = 6; x << 2 + //~^ ERROR: all if blocks contain the same code at the end + //~| NOTE: the end suggestion probably needs some adjustments to use the expressio }; if x == 9 { @@ -177,6 +186,8 @@ fn added_note_for_expression_use() -> u32 { } else { let _ = 17; x * 4 + //~^ ERROR: all if blocks contain the same code at the end + //~| NOTE: the end suggestion probably needs some adjustments to use the expressio } } @@ -189,6 +200,7 @@ fn test_suggestion_with_weird_formatting() { // 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; } + //~^ ERROR: all if blocks contain the same code at the end } fn fp_test() { diff --git a/tests/ui/branches_sharing_code/shared_at_bottom.stderr b/tests/ui/branches_sharing_code/shared_at_bottom.stderr index b9b113dc0c6..d00717befc1 100644 --- a/tests/ui/branches_sharing_code/shared_at_bottom.stderr +++ b/tests/ui/branches_sharing_code/shared_at_bottom.stderr @@ -2,6 +2,8 @@ error: all if blocks contain the same code at the end --> $DIR/shared_at_bottom.rs:31:5 | LL | / let result = false; +LL | | +LL | | LL | | println!("Block end!"); LL | | result LL | | }; @@ -17,14 +19,17 @@ help: consider moving these statements after the if | LL ~ } LL + let result = false; +LL + +LL + LL + println!("Block end!"); LL ~ result; | error: all if blocks contain the same code at the end - --> $DIR/shared_at_bottom.rs:49:5 + --> $DIR/shared_at_bottom.rs:51:5 | LL | / println!("Same end of block"); +LL | | LL | | } | |_____^ | @@ -35,9 +40,10 @@ LL + println!("Same end of block"); | error: all if blocks contain the same code at the end - --> $DIR/shared_at_bottom.rs:66:5 + --> $DIR/shared_at_bottom.rs:69:5 | LL | / println!( +LL | | LL | | "I'm moveable because I know: `outer_scope_value`: '{}'", LL | | outer_scope_value LL | | ); @@ -48,15 +54,17 @@ help: consider moving these statements after the if | LL ~ } LL + println!( +LL + 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:78:9 + --> $DIR/shared_at_bottom.rs:82:9 | LL | / println!("Hello World"); +LL | | LL | | } | |_________^ | @@ -67,9 +75,10 @@ LL + println!("Hello World"); | error: all if blocks contain the same code at the end - --> $DIR/shared_at_bottom.rs:94:5 + --> $DIR/shared_at_bottom.rs:99:5 | LL | / let later_used_value = "A string value"; +LL | | LL | | println!("{}", later_used_value); LL | | // I'm expecting a note about this LL | | } @@ -80,13 +89,15 @@ help: consider moving these statements after the if | LL ~ } LL + let later_used_value = "A string value"; +LL + LL + println!("{}", later_used_value); | error: all if blocks contain the same code at the end - --> $DIR/shared_at_bottom.rs:107:5 + --> $DIR/shared_at_bottom.rs:113:5 | LL | / let simple_examples = "I now identify as a &str :)"; +LL | | LL | | println!("This is the new simple_example: {}", simple_examples); LL | | } | |_____^ @@ -96,13 +107,16 @@ help: consider moving these statements after the if | LL ~ } LL + let simple_examples = "I now identify as a &str :)"; +LL + 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:172:5 + --> $DIR/shared_at_bottom.rs:179:5 | LL | / x << 2 +LL | | +LL | | LL | | }; | |_____^ | @@ -114,9 +128,11 @@ LL ~ x << 2; | error: all if blocks contain the same code at the end - --> $DIR/shared_at_bottom.rs:179:5 + --> $DIR/shared_at_bottom.rs:188:5 | LL | / x * 4 +LL | | +LL | | LL | | } | |_____^ | @@ -128,7 +144,7 @@ LL + x * 4 | error: all if blocks contain the same code at the end - --> $DIR/shared_at_bottom.rs:191:44 + --> $DIR/shared_at_bottom.rs:202:44 | LL | if x == 17 { b = 1; a = 0x99; } else { a = 0x99; } | ^^^^^^^^^^^ diff --git a/tests/ui/branches_sharing_code/shared_at_top.rs b/tests/ui/branches_sharing_code/shared_at_top.rs index 9e0b99f1666..44f8b2eabce 100644 --- a/tests/ui/branches_sharing_code/shared_at_top.rs +++ b/tests/ui/branches_sharing_code/shared_at_top.rs @@ -1,7 +1,7 @@ #![deny(clippy::branches_sharing_code, clippy::if_same_then_else)] #![allow(dead_code)] #![allow(clippy::mixed_read_write_in_expression, clippy::uninlined_format_args)] - +//@no-rustfix // This tests the branches_sharing_code lint at the start of blocks fn simple_examples() { @@ -9,6 +9,7 @@ fn simple_examples() { // Simple if true { + //~^ ERROR: all if blocks contain the same code at the start println!("Hello World!"); println!("I'm branch nr: 1"); } else { @@ -18,6 +19,7 @@ fn simple_examples() { // Else if if x == 0 { + //~^ ERROR: all if blocks contain the same code at the start let y = 9; println!("The value y was set to: `{}`", y); let _z = y; @@ -39,6 +41,7 @@ fn simple_examples() { // Return a value let _ = if x == 7 { + //~^ ERROR: all if blocks contain the same code at the start let y = 16; println!("What can I say except: \"you're welcome?\""); let _ = y; @@ -57,6 +60,7 @@ fn simple_but_suggestion_is_invalid() { // Can't be automatically moved because used_value_name is getting used again let used_value_name = 19; if x == 10 { + //~^ ERROR: all if blocks contain the same code at the start let used_value_name = "Different type"; println!("Str: {}", used_value_name); let _ = 1; @@ -71,6 +75,7 @@ fn simple_but_suggestion_is_invalid() { let can_be_overridden = 8; let _ = can_be_overridden; if x == 11 { + //~^ ERROR: all if blocks contain the same code at the start let can_be_overridden = "Move me"; println!("I'm also moveable"); let _ = 111; @@ -87,6 +92,7 @@ fn check_if_same_than_else_mask() { #[allow(clippy::if_same_then_else)] if x == 2020 { + //~^ ERROR: all if blocks contain the same code at the start println!("This should trigger the `SHARED_CODE_IN_IF_BLOCKS` lint."); println!("Because `IF_SAME_THEN_ELSE` is allowed here"); } else { @@ -95,6 +101,7 @@ fn check_if_same_than_else_mask() { } if x == 2019 { + //~^ ERROR: this `if` has identical blocks println!("This should trigger `IS_SAME_THAN_ELSE` as usual"); } else { println!("This should trigger `IS_SAME_THAN_ELSE` as usual"); diff --git a/tests/ui/branches_sharing_code/shared_at_top.stderr b/tests/ui/branches_sharing_code/shared_at_top.stderr index 3e3242a75d3..9d4d42fb689 100644 --- a/tests/ui/branches_sharing_code/shared_at_top.stderr +++ b/tests/ui/branches_sharing_code/shared_at_top.stderr @@ -2,6 +2,7 @@ error: all if blocks contain the same code at the start --> $DIR/shared_at_top.rs:11:5 | LL | / if true { +LL | | LL | | println!("Hello World!"); | |_________________________________^ | @@ -17,9 +18,10 @@ LL + if true { | error: all if blocks contain the same code at the start - --> $DIR/shared_at_top.rs:20:5 + --> $DIR/shared_at_top.rs:21:5 | LL | / if x == 0 { +LL | | LL | | let y = 9; LL | | println!("The value y was set to: `{}`", y); LL | | let _z = y; @@ -35,9 +37,10 @@ LL + if x == 0 { | error: all if blocks contain the same code at the start - --> $DIR/shared_at_top.rs:41:5 + --> $DIR/shared_at_top.rs:43:5 | LL | / let _ = if x == 7 { +LL | | LL | | let y = 16; | |___________________^ | @@ -48,9 +51,10 @@ LL + let _ = if x == 7 { | error: all if blocks contain the same code at the start - --> $DIR/shared_at_top.rs:59:5 + --> $DIR/shared_at_top.rs:62:5 | LL | / if x == 10 { +LL | | LL | | let used_value_name = "Different type"; LL | | println!("Str: {}", used_value_name); | |_____________________________________________^ @@ -64,9 +68,10 @@ LL + if x == 10 { | error: all if blocks contain the same code at the start - --> $DIR/shared_at_top.rs:73:5 + --> $DIR/shared_at_top.rs:77:5 | LL | / if x == 11 { +LL | | LL | | let can_be_overridden = "Move me"; LL | | println!("I'm also moveable"); | |______________________________________^ @@ -80,9 +85,10 @@ LL + if x == 11 { | error: all if blocks contain the same code at the start - --> $DIR/shared_at_top.rs:89:5 + --> $DIR/shared_at_top.rs:94:5 | LL | / if x == 2020 { +LL | | LL | | println!("This should trigger the `SHARED_CODE_IN_IF_BLOCKS` lint."); LL | | println!("Because `IF_SAME_THEN_ELSE` is allowed here"); | |________________________________________________________________^ @@ -95,16 +101,17 @@ LL + if x == 2020 { | error: this `if` has identical blocks - --> $DIR/shared_at_top.rs:97:18 + --> $DIR/shared_at_top.rs:103:18 | LL | if x == 2019 { | __________________^ +LL | | LL | | println!("This should trigger `IS_SAME_THAN_ELSE` as usual"); LL | | } else { | |_____^ | note: same as this - --> $DIR/shared_at_top.rs:99:12 + --> $DIR/shared_at_top.rs:106:12 | LL | } else { | ____________^ 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 index 93b8c6e10da..36620ee1a9b 100644 --- a/tests/ui/branches_sharing_code/shared_at_top_and_bottom.rs +++ b/tests/ui/branches_sharing_code/shared_at_top_and_bottom.rs @@ -1,7 +1,7 @@ #![deny(clippy::branches_sharing_code, clippy::if_same_then_else)] #![allow(dead_code)] #![allow(clippy::uninlined_format_args)] - +//@no-rustfix // branches_sharing_code at the top and bottom of the if blocks struct DataPack { @@ -15,6 +15,7 @@ fn overlapping_eq_regions() { // Overlap with separator if x == 7 { + //~^ ERROR: all if blocks contain the same code at both the start and the end let t = 7; let _overlap_start = t * 2; let _overlap_end = 2 * t; @@ -31,6 +32,7 @@ fn overlapping_eq_regions() { // Overlap with separator if x == 99 { + //~^ ERROR: all if blocks contain the same code at both the start and the end let r = 7; let _overlap_start = r; let _overlap_middle = r * r; @@ -60,6 +62,7 @@ fn complexer_example() { let x = 8; let y = 9; if (x > 7 && y < 13) || (x + y) % 2 == 1 { + //~^ ERROR: all if blocks contain the same code at both the start and the end let a = 0xcafe; let b = 0xffff00ff; let e_id = gen_id(a, b); @@ -93,6 +96,7 @@ fn added_note_for_expression_use() -> u32 { let x = 9; let _ = if x == 7 { + //~^ ERROR: all if blocks contain the same code at both the start and the end let _ = 19; let _splitter = 6; @@ -105,6 +109,7 @@ fn added_note_for_expression_use() -> u32 { }; if x == 9 { + //~^ ERROR: all if blocks contain the same code at both the start and the end let _ = 17; let _splitter = 6; 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 index ccd697a4215..74495fca8ab 100644 --- a/tests/ui/branches_sharing_code/shared_at_top_and_bottom.stderr +++ b/tests/ui/branches_sharing_code/shared_at_top_and_bottom.stderr @@ -2,13 +2,14 @@ error: all if blocks contain the same code at both the start and the end --> $DIR/shared_at_top_and_bottom.rs:17:5 | LL | / if x == 7 { +LL | | LL | | let t = 7; LL | | let _overlap_start = t * 2; LL | | let _overlap_end = 2 * t; | |_________________________________^ | note: this code is shared at the end - --> $DIR/shared_at_top_and_bottom.rs:29:5 + --> $DIR/shared_at_top_and_bottom.rs:30:5 | LL | / let _u = 9; LL | | } @@ -32,16 +33,17 @@ LL + let _u = 9; | error: all if blocks contain the same code at both the start and the end - --> $DIR/shared_at_top_and_bottom.rs:33:5 + --> $DIR/shared_at_top_and_bottom.rs:34:5 | LL | / if x == 99 { +LL | | LL | | let r = 7; LL | | let _overlap_start = r; LL | | let _overlap_middle = r * r; | |____________________________________^ | note: this code is shared at the end - --> $DIR/shared_at_top_and_bottom.rs:44:5 + --> $DIR/shared_at_top_and_bottom.rs:46:5 | LL | / let _overlap_end = r * r * r; LL | | let z = "end"; @@ -63,16 +65,17 @@ LL + let z = "end"; | error: all if blocks contain the same code at both the start and the end - --> $DIR/shared_at_top_and_bottom.rs:62:5 + --> $DIR/shared_at_top_and_bottom.rs:64:5 | LL | / if (x > 7 && y < 13) || (x + y) % 2 == 1 { +LL | | LL | | let a = 0xcafe; LL | | let b = 0xffff00ff; LL | | let e_id = gen_id(a, b); | |________________________________^ | note: this code is shared at the end - --> $DIR/shared_at_top_and_bottom.rs:82:5 + --> $DIR/shared_at_top_and_bottom.rs:85:5 | LL | / let pack = DataPack { LL | | id: e_id, @@ -102,14 +105,15 @@ LL + process_data(pack); | error: all if blocks contain the same code at both the start and the end - --> $DIR/shared_at_top_and_bottom.rs:95:5 + --> $DIR/shared_at_top_and_bottom.rs:98:5 | LL | / let _ = if x == 7 { +LL | | LL | | let _ = 19; | |___________________^ | note: this code is shared at the end - --> $DIR/shared_at_top_and_bottom.rs:104:5 + --> $DIR/shared_at_top_and_bottom.rs:108:5 | LL | / x << 2 LL | | }; @@ -127,14 +131,15 @@ LL ~ x << 2; | error: all if blocks contain the same code at both the start and the end - --> $DIR/shared_at_top_and_bottom.rs:107:5 + --> $DIR/shared_at_top_and_bottom.rs:111:5 | LL | / if x == 9 { +LL | | LL | | let _ = 17; | |___________________^ | note: this code is shared at the end - --> $DIR/shared_at_top_and_bottom.rs:116:5 + --> $DIR/shared_at_top_and_bottom.rs:121:5 | LL | / x * 4 LL | | } diff --git a/tests/ui/branches_sharing_code/valid_if_blocks.rs b/tests/ui/branches_sharing_code/valid_if_blocks.rs index 5780ea08937..2aeacb89c0c 100644 --- a/tests/ui/branches_sharing_code/valid_if_blocks.rs +++ b/tests/ui/branches_sharing_code/valid_if_blocks.rs @@ -107,6 +107,7 @@ fn valid_examples() { // Let's test empty blocks if false { + //~^ ERROR: this `if` has identical blocks } else { } } @@ -118,6 +119,7 @@ fn trigger_other_lint() { // Same block if x == 0 { + //~^ ERROR: this `if` has identical blocks let u = 19; println!("How are u today?"); let _ = "This is a string"; @@ -129,12 +131,14 @@ fn trigger_other_lint() { // Only same expression let _ = if x == 6 { 7 } else { 7 }; + //~^ ERROR: this `if` has identical blocks // 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 { + //~^ ERROR: this `if` has identical blocks println!("I'm a doppelgänger"); // Don't listen to my clone below @@ -149,6 +153,7 @@ fn trigger_other_lint() { if x == 0 { println!("I'm single"); } else if x == 68 { + //~^ ERROR: this `if` has identical blocks println!("I'm a doppelgänger"); // Don't listen to my clone below } else { diff --git a/tests/ui/branches_sharing_code/valid_if_blocks.stderr b/tests/ui/branches_sharing_code/valid_if_blocks.stderr index a7e72b780af..fcbf12235aa 100644 --- a/tests/ui/branches_sharing_code/valid_if_blocks.stderr +++ b/tests/ui/branches_sharing_code/valid_if_blocks.stderr @@ -3,11 +3,12 @@ error: this `if` has identical blocks | LL | if false { | ______________^ +LL | | LL | | } else { | |_____^ | note: same as this - --> $DIR/valid_if_blocks.rs:110:12 + --> $DIR/valid_if_blocks.rs:111:12 | LL | } else { | ____________^ @@ -20,10 +21,11 @@ LL | #![deny(clippy::branches_sharing_code, clippy::if_same_then_else)] | ^^^^^^^^^^^^^^^^^^^^^^^^^ error: this `if` has identical blocks - --> $DIR/valid_if_blocks.rs:120:15 + --> $DIR/valid_if_blocks.rs:121:15 | LL | if x == 0 { | _______________^ +LL | | LL | | let u = 19; LL | | println!("How are u today?"); LL | | let _ = "This is a string"; @@ -31,7 +33,7 @@ LL | | } else { | |_____^ | note: same as this - --> $DIR/valid_if_blocks.rs:124:12 + --> $DIR/valid_if_blocks.rs:126:12 | LL | } else { | ____________^ @@ -42,22 +44,23 @@ LL | | } | |_____^ error: this `if` has identical blocks - --> $DIR/valid_if_blocks.rs:131:23 + --> $DIR/valid_if_blocks.rs:133:23 | LL | let _ = if x == 6 { 7 } else { 7 }; | ^^^^^ | note: same as this - --> $DIR/valid_if_blocks.rs:131:34 + --> $DIR/valid_if_blocks.rs:133:34 | LL | let _ = if x == 6 { 7 } else { 7 }; | ^^^^^ error: this `if` has identical blocks - --> $DIR/valid_if_blocks.rs:137:23 + --> $DIR/valid_if_blocks.rs:140:23 | LL | } else if x == 68 { | _______________________^ +LL | | LL | | println!("I'm a doppelgänger"); LL | | // Don't listen to my clone below LL | | @@ -66,7 +69,7 @@ LL | | } else { | |_____^ | note: same as this - --> $DIR/valid_if_blocks.rs:142:12 + --> $DIR/valid_if_blocks.rs:146:12 | LL | } else { | ____________^ @@ -78,17 +81,18 @@ LL | | }; | |_____^ error: this `if` has identical blocks - --> $DIR/valid_if_blocks.rs:151:23 + --> $DIR/valid_if_blocks.rs:155:23 | LL | } else if x == 68 { | _______________________^ +LL | | 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:154:12 + --> $DIR/valid_if_blocks.rs:159:12 | LL | } else { | ____________^ |
