Rework branches_sharing_code

This commit is contained in:
Jason Newcomb 2022-05-26 13:57:05 -04:00
parent 91644d1f1d
commit 7975d41a91
7 changed files with 376 additions and 468 deletions

View file

@ -25,4 +25,17 @@ impl FooBar {
fn baz(&mut self) {}
}
fn main() {}
fn foo(x: u32, y: u32) -> u32 {
x / y
}
fn main() {
let x = (1, 2);
let _ = if true {
let (x, y) = x;
foo(x, y)
} else {
let (y, x) = x;
foo(x, y)
};
}

View file

@ -12,8 +12,8 @@ note: the lint level is defined here
|
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
= note: the end suggestion probably needs some adjustments to use the expression result correctly
help: consider moving these statements after the if
|
LL ~ }
LL + let result = false;
@ -28,7 +28,7 @@ LL | / println!("Same end of block");
LL | | }
| |_____^
|
help: consider moving the end statements out like this
help: consider moving these statements after the if
|
LL ~ }
LL + println!("Same end of block");
@ -44,7 +44,7 @@ LL | | );
LL | | }
| |_____^
|
help: consider moving the end statements out like this
help: consider moving these statements after the if
|
LL ~ }
LL + println!(
@ -60,7 +60,7 @@ LL | / println!("Hello World");
LL | | }
| |_________^
|
help: consider moving the end statements out like this
help: consider moving these statements after the if
|
LL ~ }
LL + println!("Hello World");
@ -75,8 +75,8 @@ 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
= warning: some moved values might need to be renamed to avoid wrong references
help: consider moving these statements after the if
|
LL ~ }
LL + let later_used_value = "A string value";
@ -91,8 +91,8 @@ 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
= warning: some moved values might need to be renamed to avoid wrong references
help: consider moving these statements after the if
|
LL ~ }
LL + let simple_examples = "I now identify as a &str :)";
@ -106,8 +106,8 @@ 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
= note: the end suggestion probably needs some adjustments to use the expression result correctly
help: consider moving these statements after the if
|
LL ~ }
LL ~ x << 2;
@ -120,8 +120,8 @@ 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
= note: the end suggestion probably needs some adjustments to use the expression result correctly
help: consider moving these statements after the if
|
LL ~ }
LL + x * 4
@ -133,7 +133,7 @@ error: all if blocks contain the same code at the end
LL | if x == 17 { b = 1; a = 0x99; } else { a = 0x99; }
| ^^^^^^^^^^^
|
help: consider moving the end statements out like this
help: consider moving these statements after the if
|
LL ~ if x == 17 { b = 1; a = 0x99; } else { }
LL + a = 0x99;

View file

@ -10,7 +10,7 @@ note: the lint level is defined here
|
LL | #![deny(clippy::if_same_then_else, clippy::branches_sharing_code)]
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
help: consider moving the start statements out like this
help: consider moving these statements before the if
|
LL ~ println!("Hello World!");
LL + if true {
@ -25,8 +25,8 @@ 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
= warning: some moved values might need to be renamed to avoid wrong references
help: consider moving these statements before the if
|
LL ~ let y = 9;
LL + println!("The value y was set to: `{}`", y);
@ -41,7 +41,7 @@ LL | / let _ = if x == 7 {
LL | | let y = 16;
| |___________________^
|
help: consider moving the start statements out like this
help: consider moving these statements before the if
|
LL ~ let y = 16;
LL + let _ = if x == 7 {
@ -55,8 +55,8 @@ 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
= warning: some moved values might need to be renamed to avoid wrong references
help: consider moving these statements before the if
|
LL ~ let used_value_name = "Different type";
LL + println!("Str: {}", used_value_name);
@ -71,8 +71,8 @@ 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
= warning: some moved values might need to be renamed to avoid wrong references
help: consider moving these statements before the if
|
LL ~ let can_be_overridden = "Move me";
LL + println!("I'm also moveable");
@ -87,7 +87,7 @@ 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
help: consider moving these statements before the if
|
LL ~ println!("This should trigger the `SHARED_CODE_IN_IF_BLOCKS` lint.");
LL + println!("Because `IF_SAME_THEN_ELSE` is allowed here");

View file

@ -1,4 +1,4 @@
error: all if blocks contain the same code at the start and the end. Here at the start
error: all if blocks contain the same code at both the start and the end
--> $DIR/shared_at_top_and_bottom.rs:16:5
|
LL | / if x == 7 {
@ -12,26 +12,26 @@ note: the lint level is defined here
|
LL | #![deny(clippy::if_same_then_else, clippy::branches_sharing_code)]
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
note: and here at the end
note: this code is shared 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
help: consider moving these statements before the if
|
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
help: consider moving these statements after the if
|
LL ~ }
LL + let _u = 9;
|
error: all if blocks contain the same code at the start and the end. Here at the start
error: all if blocks contain the same code at both the start and the end
--> $DIR/shared_at_top_and_bottom.rs:32:5
|
LL | / if x == 99 {
@ -40,29 +40,29 @@ LL | | let _overlap_start = r;
LL | | let _overlap_middle = r * r;
| |____________________________________^
|
note: and here at the end
note: this code is shared 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
= warning: some moved values might need to be renamed to avoid wrong references
help: consider moving these statements before the if
|
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
help: consider moving these statements after the if
|
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
error: all if blocks contain the same code at both the start and the end
--> $DIR/shared_at_top_and_bottom.rs:61:5
|
LL | / if (x > 7 && y < 13) || (x + y) % 2 == 1 {
@ -71,7 +71,7 @@ LL | | let b = 0xffff00ff;
LL | | let e_id = gen_id(a, b);
| |________________________________^
|
note: and here at the end
note: this code is shared at the end
--> $DIR/shared_at_top_and_bottom.rs:81:5
|
LL | / let pack = DataPack {
@ -82,15 +82,15 @@ 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
= warning: some moved values might need to be renamed to avoid wrong references
help: consider moving these statements before the if
|
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
help: consider moving these statements after the if
|
LL ~ }
LL + let pack = DataPack {
@ -100,51 +100,51 @@ 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
error: all if blocks contain the same code at both the start and the end
--> $DIR/shared_at_top_and_bottom.rs:94:5
|
LL | / let _ = if x == 7 {
LL | | let _ = 19;
| |___________________^
|
note: and here at the end
note: this code is shared 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
= note: the end suggestion probably needs some adjustments to use the expression result correctly
help: consider moving these statements before the if
|
LL ~ let _ = 19;
LL + let _ = if x == 7 {
|
help: and consider moving the end statements out like this
help: consider moving these statements after the if
|
LL ~ }
LL ~ x << 2;
|
error: all if blocks contain the same code at the start and the end. Here at the start
error: all if blocks contain the same code at both the start and the end
--> $DIR/shared_at_top_and_bottom.rs:106:5
|
LL | / if x == 9 {
LL | | let _ = 17;
| |___________________^
|
note: and here at the end
note: this code is shared 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
= note: the end suggestion probably needs some adjustments to use the expression result correctly
help: consider moving these statements before the if
|
LL ~ let _ = 17;
LL + if x == 9 {
|
help: and consider moving the end statements out like this
help: consider moving these statements after the if
|
LL ~ }
LL + x * 4