2021-10-04 01:33:40 -05:00
|
|
|
#![allow(dead_code, clippy::equatable_if_let)]
|
2021-04-01 11:30:47 -05:00
|
|
|
#![deny(clippy::if_same_then_else, clippy::branches_sharing_code)]
|
2020-12-11 16:29:53 -06:00
|
|
|
|
2021-04-01 11:30:47 -05:00
|
|
|
// This tests the branches_sharing_code lint at the end of blocks
|
2020-12-11 16:29:53 -06:00
|
|
|
|
|
|
|
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
|
|
|
|
};
|
2021-01-13 13:01:15 -06:00
|
|
|
|
2021-01-16 07:04:14 -06:00
|
|
|
// Else if block
|
2021-01-13 13:01:15 -06:00
|
|
|
if x == 9 {
|
|
|
|
println!("The index is: 6");
|
|
|
|
|
|
|
|
println!("Same end of block");
|
|
|
|
} else if x == 8 {
|
|
|
|
println!("The index is: 4");
|
|
|
|
|
2021-01-16 07:04:14 -06:00
|
|
|
// We should only get a lint trigger for the last statement
|
|
|
|
println!("This is also eq with the else block");
|
2021-01-13 13:01:15 -06:00
|
|
|
println!("Same end of block");
|
|
|
|
} else {
|
2021-01-16 07:04:14 -06:00
|
|
|
println!("This is also eq with the else block");
|
2021-02-23 14:16:19 -06:00
|
|
|
println!("Same end of block");
|
2021-01-16 07:04:14 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
// 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
|
|
|
|
);
|
2021-01-13 13:01:15 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
if x == 9 {
|
|
|
|
if x == 8 {
|
|
|
|
// No parent!!
|
|
|
|
println!("---");
|
2021-02-23 14:16:19 -06:00
|
|
|
println!("Hello World");
|
2021-01-13 13:01:15 -06:00
|
|
|
} else {
|
|
|
|
println!("Hello World");
|
|
|
|
}
|
|
|
|
}
|
2020-12-11 16:29:53 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Simple examples where the move can cause some problems due to moved values
|
|
|
|
fn simple_but_suggestion_is_invalid() {
|
2021-01-13 13:01:15 -06:00
|
|
|
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();
|
2020-12-11 16:29:53 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Tests where the blocks are not linted due to the used value scope
|
|
|
|
fn not_moveable_due_to_value_scope() {
|
2021-01-13 13:01:15 -06:00
|
|
|
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
|
|
|
|
};
|
2020-12-11 16:29:53 -06:00
|
|
|
}
|
|
|
|
|
2021-01-16 13:37:50 -06:00
|
|
|
/// 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
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-01-16 07:04:14 -06:00
|
|
|
#[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; }
|
|
|
|
}
|
|
|
|
|
2021-02-25 16:33:46 -06:00
|
|
|
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")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-04-10 07:45:51 -05:00
|
|
|
fn fp_if_let_issue7054() {
|
|
|
|
// This shouldn't trigger the lint
|
|
|
|
let string;
|
|
|
|
let _x = if let true = true {
|
|
|
|
""
|
|
|
|
} else if true {
|
|
|
|
string = "x".to_owned();
|
|
|
|
&string
|
|
|
|
} else {
|
|
|
|
string = "y".to_owned();
|
|
|
|
&string
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2020-12-11 16:29:53 -06:00
|
|
|
fn main() {}
|