Moving shared_code_in_if_blocks to clippy::complexity and running lintcheck

This commit is contained in:
xFrednet 2021-02-25 23:33:46 +01:00
parent c74e49eab9
commit 617c65baa9
21 changed files with 180 additions and 84 deletions

View file

@ -142,7 +142,7 @@ declare_clippy_lint! {
/// }; /// };
/// ``` /// ```
pub SHARED_CODE_IN_IF_BLOCKS, pub SHARED_CODE_IN_IF_BLOCKS,
nursery, complexity,
"`if` statement with shared code in all blocks" "`if` statement with shared code in all blocks"
} }
@ -457,11 +457,11 @@ fn emit_shared_code_in_if_blocks_lint(
let add_optional_msgs = |diag: &mut DiagnosticBuilder<'_>| { let add_optional_msgs = |diag: &mut DiagnosticBuilder<'_>| {
if add_expr_note { if add_expr_note {
diag.note("The end suggestion probably needs some adjustments to use the expression result correctly."); diag.note("The end suggestion probably needs some adjustments to use the expression result correctly");
} }
if warn_about_moved_symbol { if warn_about_moved_symbol {
diag.warn("Some moved values might need to be renamed to avoid wrong references."); diag.warn("Some moved values might need to be renamed to avoid wrong references");
} }
}; };

View file

@ -1485,6 +1485,7 @@ pub fn register_plugins(store: &mut rustc_lint::LintStore, sess: &Session, conf:
LintId::of(&comparison_chain::COMPARISON_CHAIN), LintId::of(&comparison_chain::COMPARISON_CHAIN),
LintId::of(&copies::IFS_SAME_COND), LintId::of(&copies::IFS_SAME_COND),
LintId::of(&copies::IF_SAME_THEN_ELSE), LintId::of(&copies::IF_SAME_THEN_ELSE),
LintId::of(&copies::SHARED_CODE_IN_IF_BLOCKS),
LintId::of(&default::FIELD_REASSIGN_WITH_DEFAULT), LintId::of(&default::FIELD_REASSIGN_WITH_DEFAULT),
LintId::of(&derive::DERIVE_HASH_XOR_EQ), LintId::of(&derive::DERIVE_HASH_XOR_EQ),
LintId::of(&derive::DERIVE_ORD_XOR_PARTIAL_ORD), LintId::of(&derive::DERIVE_ORD_XOR_PARTIAL_ORD),
@ -2063,7 +2064,6 @@ pub fn register_plugins(store: &mut rustc_lint::LintStore, sess: &Session, conf:
store.register_group(true, "clippy::nursery", Some("clippy_nursery"), vec![ store.register_group(true, "clippy::nursery", Some("clippy_nursery"), vec![
LintId::of(&attrs::EMPTY_LINE_AFTER_OUTER_ATTR), LintId::of(&attrs::EMPTY_LINE_AFTER_OUTER_ATTR),
LintId::of(&cognitive_complexity::COGNITIVE_COMPLEXITY), LintId::of(&cognitive_complexity::COGNITIVE_COMPLEXITY),
LintId::of(&copies::SHARED_CODE_IN_IF_BLOCKS),
LintId::of(&disallowed_method::DISALLOWED_METHOD), LintId::of(&disallowed_method::DISALLOWED_METHOD),
LintId::of(&fallible_impl_from::FALLIBLE_IMPL_FROM), LintId::of(&fallible_impl_from::FALLIBLE_IMPL_FROM),
LintId::of(&floating_point_arithmetic::IMPRECISE_FLOPS), LintId::of(&floating_point_arithmetic::IMPRECISE_FLOPS),

View file

@ -96,9 +96,11 @@ impl HirEqInterExpr<'_, '_, '_> {
pub fn eq_stmt(&mut self, left: &Stmt<'_>, right: &Stmt<'_>) -> bool { pub fn eq_stmt(&mut self, left: &Stmt<'_>, right: &Stmt<'_>) -> bool {
match (&left.kind, &right.kind) { match (&left.kind, &right.kind) {
(&StmtKind::Local(ref l), &StmtKind::Local(ref r)) => { (&StmtKind::Local(ref l), &StmtKind::Local(ref r)) => {
self.eq_pat(&l.pat, &r.pat) // eq_pat adds the HirIds to the locals map. We therefor call it last to make sure that
// these only get added if the init and type is equal.
both(&l.init, &r.init, |l, r| self.eq_expr(l, r))
&& both(&l.ty, &r.ty, |l, r| self.eq_ty(l, r)) && both(&l.ty, &r.ty, |l, r| self.eq_ty(l, r))
&& both(&l.init, &r.init, |l, r| self.eq_expr(l, r)) && self.eq_pat(&l.pat, &r.pat)
}, },
(&StmtKind::Expr(ref l), &StmtKind::Expr(ref r)) | (&StmtKind::Semi(ref l), &StmtKind::Semi(ref r)) => { (&StmtKind::Expr(ref l), &StmtKind::Expr(ref r)) | (&StmtKind::Semi(ref l), &StmtKind::Semi(ref r)) => {
self.eq_expr(l, r) self.eq_expr(l, r)

View file

@ -609,9 +609,9 @@ pub fn get_pat_name(pat: &Pat<'_>) -> Option<Symbol> {
} }
} }
struct ContainsName { pub struct ContainsName {
name: Symbol, pub name: Symbol,
result: bool, pub result: bool,
} }
impl<'tcx> Visitor<'tcx> for ContainsName { impl<'tcx> Visitor<'tcx> for ContainsName {
@ -1216,6 +1216,8 @@ pub fn if_sequence<'tcx>(mut expr: &'tcx Expr<'tcx>) -> (Vec<&'tcx Expr<'tcx>>,
(conds, blocks) (conds, blocks)
} }
/// This function returns true if the given expression is the `else` or `if else` part of an if
/// statement
pub fn parent_node_is_if_expr(expr: &Expr<'_>, cx: &LateContext<'_>) -> bool { pub fn parent_node_is_if_expr(expr: &Expr<'_>, cx: &LateContext<'_>) -> bool {
let map = cx.tcx.hir(); let map = cx.tcx.hir();
let parent_id = map.get_parent_node(expr.hir_id); let parent_id = map.get_parent_node(expr.hir_id);
@ -1326,6 +1328,16 @@ pub fn fn_def_id(cx: &LateContext<'_>, expr: &Expr<'_>) -> Option<DefId> {
} }
} }
/// This function checks if any of the lints in the slice is enabled for the provided `HirId`.
/// A lint counts as enabled with any of the levels: `Level::Forbid` | `Level::Deny` | `Level::Warn`
///
/// ```ignore
/// #[deny(clippy::YOUR_AWESOME_LINT)]
/// println!("Hello, World!"); // <- Clippy code: run_lints(cx, &[YOUR_AWESOME_LINT], id) == true
///
/// #[allow(clippy::YOUR_AWESOME_LINT)]
/// println!("See you soon!"); // <- Clippy code: run_lints(cx, &[YOUR_AWESOME_LINT], id) == false
/// ```
pub fn run_lints(cx: &LateContext<'_>, lints: &[&'static Lint], id: HirId) -> bool { pub fn run_lints(cx: &LateContext<'_>, lints: &[&'static Lint], id: HirId) -> bool {
lints.iter().any(|lint| { lints.iter().any(|lint| {
matches!( matches!(

View file

@ -1,5 +1,5 @@
#![deny(clippy::panicking_unwrap, clippy::unnecessary_unwrap)] #![deny(clippy::panicking_unwrap, clippy::unnecessary_unwrap)]
#![allow(clippy::if_same_then_else)] #![allow(clippy::if_same_then_else, clippy::shared_code_in_if_blocks)]
fn test_nested() { fn test_nested() {
fn nested() { fn nested() {

View file

@ -1,5 +1,5 @@
#![deny(clippy::panicking_unwrap, clippy::unnecessary_unwrap)] #![deny(clippy::panicking_unwrap, clippy::unnecessary_unwrap)]
#![allow(clippy::if_same_then_else)] #![allow(clippy::if_same_then_else, clippy::shared_code_in_if_blocks)]
macro_rules! m { macro_rules! m {
($a:expr) => { ($a:expr) => {

View file

@ -3,6 +3,7 @@
#![allow(clippy::never_loop)] #![allow(clippy::never_loop)]
#![allow(clippy::no_effect)] #![allow(clippy::no_effect)]
#![allow(clippy::unnecessary_operation)] #![allow(clippy::unnecessary_operation)]
#![allow(clippy::shared_code_in_if_blocks)]
mod basic_expr { mod basic_expr {
fn test() { fn test() {

View file

@ -1,5 +1,5 @@
error: default numeric fallback might occur error: default numeric fallback might occur
--> $DIR/default_numeric_fallback.rs:10:17 --> $DIR/default_numeric_fallback.rs:11:17
| |
LL | let x = 22; LL | let x = 22;
| ^^ help: consider adding suffix: `22_i32` | ^^ help: consider adding suffix: `22_i32`
@ -7,139 +7,139 @@ LL | let x = 22;
= note: `-D clippy::default-numeric-fallback` implied by `-D warnings` = note: `-D clippy::default-numeric-fallback` implied by `-D warnings`
error: default numeric fallback might occur error: default numeric fallback might occur
--> $DIR/default_numeric_fallback.rs:11:18 --> $DIR/default_numeric_fallback.rs:12:18
| |
LL | let x = [1, 2, 3]; LL | let x = [1, 2, 3];
| ^ help: consider adding suffix: `1_i32` | ^ help: consider adding suffix: `1_i32`
error: default numeric fallback might occur error: default numeric fallback might occur
--> $DIR/default_numeric_fallback.rs:11:21 --> $DIR/default_numeric_fallback.rs:12:21
| |
LL | let x = [1, 2, 3]; LL | let x = [1, 2, 3];
| ^ help: consider adding suffix: `2_i32` | ^ help: consider adding suffix: `2_i32`
error: default numeric fallback might occur error: default numeric fallback might occur
--> $DIR/default_numeric_fallback.rs:11:24 --> $DIR/default_numeric_fallback.rs:12:24
| |
LL | let x = [1, 2, 3]; LL | let x = [1, 2, 3];
| ^ help: consider adding suffix: `3_i32` | ^ help: consider adding suffix: `3_i32`
error: default numeric fallback might occur error: default numeric fallback might occur
--> $DIR/default_numeric_fallback.rs:12:28 --> $DIR/default_numeric_fallback.rs:13:28
| |
LL | let x = if true { (1, 2) } else { (3, 4) }; LL | let x = if true { (1, 2) } else { (3, 4) };
| ^ help: consider adding suffix: `1_i32` | ^ help: consider adding suffix: `1_i32`
error: default numeric fallback might occur error: default numeric fallback might occur
--> $DIR/default_numeric_fallback.rs:12:31 --> $DIR/default_numeric_fallback.rs:13:31
| |
LL | let x = if true { (1, 2) } else { (3, 4) }; LL | let x = if true { (1, 2) } else { (3, 4) };
| ^ help: consider adding suffix: `2_i32` | ^ help: consider adding suffix: `2_i32`
error: default numeric fallback might occur error: default numeric fallback might occur
--> $DIR/default_numeric_fallback.rs:12:44 --> $DIR/default_numeric_fallback.rs:13:44
| |
LL | let x = if true { (1, 2) } else { (3, 4) }; LL | let x = if true { (1, 2) } else { (3, 4) };
| ^ help: consider adding suffix: `3_i32` | ^ help: consider adding suffix: `3_i32`
error: default numeric fallback might occur error: default numeric fallback might occur
--> $DIR/default_numeric_fallback.rs:12:47 --> $DIR/default_numeric_fallback.rs:13:47
| |
LL | let x = if true { (1, 2) } else { (3, 4) }; LL | let x = if true { (1, 2) } else { (3, 4) };
| ^ help: consider adding suffix: `4_i32` | ^ help: consider adding suffix: `4_i32`
error: default numeric fallback might occur error: default numeric fallback might occur
--> $DIR/default_numeric_fallback.rs:13:23 --> $DIR/default_numeric_fallback.rs:14:23
| |
LL | let x = match 1 { LL | let x = match 1 {
| ^ help: consider adding suffix: `1_i32` | ^ help: consider adding suffix: `1_i32`
error: default numeric fallback might occur error: default numeric fallback might occur
--> $DIR/default_numeric_fallback.rs:14:13 --> $DIR/default_numeric_fallback.rs:15:13
| |
LL | 1 => 1, LL | 1 => 1,
| ^ help: consider adding suffix: `1_i32` | ^ help: consider adding suffix: `1_i32`
error: default numeric fallback might occur error: default numeric fallback might occur
--> $DIR/default_numeric_fallback.rs:14:18 --> $DIR/default_numeric_fallback.rs:15:18
| |
LL | 1 => 1, LL | 1 => 1,
| ^ help: consider adding suffix: `1_i32` | ^ help: consider adding suffix: `1_i32`
error: default numeric fallback might occur error: default numeric fallback might occur
--> $DIR/default_numeric_fallback.rs:15:18 --> $DIR/default_numeric_fallback.rs:16:18
| |
LL | _ => 2, LL | _ => 2,
| ^ help: consider adding suffix: `2_i32` | ^ help: consider adding suffix: `2_i32`
error: default numeric fallback might occur error: default numeric fallback might occur
--> $DIR/default_numeric_fallback.rs:19:17 --> $DIR/default_numeric_fallback.rs:20:17
| |
LL | let x = 0.12; LL | let x = 0.12;
| ^^^^ help: consider adding suffix: `0.12_f64` | ^^^^ help: consider adding suffix: `0.12_f64`
error: default numeric fallback might occur error: default numeric fallback might occur
--> $DIR/default_numeric_fallback.rs:37:21 --> $DIR/default_numeric_fallback.rs:38:21
| |
LL | let y = 1; LL | let y = 1;
| ^ help: consider adding suffix: `1_i32` | ^ help: consider adding suffix: `1_i32`
error: default numeric fallback might occur error: default numeric fallback might occur
--> $DIR/default_numeric_fallback.rs:45:21 --> $DIR/default_numeric_fallback.rs:46:21
| |
LL | let y = 1; LL | let y = 1;
| ^ help: consider adding suffix: `1_i32` | ^ help: consider adding suffix: `1_i32`
error: default numeric fallback might occur error: default numeric fallback might occur
--> $DIR/default_numeric_fallback.rs:51:21 --> $DIR/default_numeric_fallback.rs:52:21
| |
LL | let y = 1; LL | let y = 1;
| ^ help: consider adding suffix: `1_i32` | ^ help: consider adding suffix: `1_i32`
error: default numeric fallback might occur error: default numeric fallback might occur
--> $DIR/default_numeric_fallback.rs:63:9 --> $DIR/default_numeric_fallback.rs:64:9
| |
LL | 1 LL | 1
| ^ help: consider adding suffix: `1_i32` | ^ help: consider adding suffix: `1_i32`
error: default numeric fallback might occur error: default numeric fallback might occur
--> $DIR/default_numeric_fallback.rs:69:27 --> $DIR/default_numeric_fallback.rs:70:27
| |
LL | let f = || -> _ { 1 }; LL | let f = || -> _ { 1 };
| ^ help: consider adding suffix: `1_i32` | ^ help: consider adding suffix: `1_i32`
error: default numeric fallback might occur error: default numeric fallback might occur
--> $DIR/default_numeric_fallback.rs:73:29 --> $DIR/default_numeric_fallback.rs:74:29
| |
LL | let f = || -> i32 { 1 }; LL | let f = || -> i32 { 1 };
| ^ help: consider adding suffix: `1_i32` | ^ help: consider adding suffix: `1_i32`
error: default numeric fallback might occur error: default numeric fallback might occur
--> $DIR/default_numeric_fallback.rs:87:21 --> $DIR/default_numeric_fallback.rs:88:21
| |
LL | generic_arg(1); LL | generic_arg(1);
| ^ help: consider adding suffix: `1_i32` | ^ help: consider adding suffix: `1_i32`
error: default numeric fallback might occur error: default numeric fallback might occur
--> $DIR/default_numeric_fallback.rs:90:32 --> $DIR/default_numeric_fallback.rs:91:32
| |
LL | let x: _ = generic_arg(1); LL | let x: _ = generic_arg(1);
| ^ help: consider adding suffix: `1_i32` | ^ help: consider adding suffix: `1_i32`
error: default numeric fallback might occur error: default numeric fallback might occur
--> $DIR/default_numeric_fallback.rs:108:28 --> $DIR/default_numeric_fallback.rs:109:28
| |
LL | GenericStruct { x: 1 }; LL | GenericStruct { x: 1 };
| ^ help: consider adding suffix: `1_i32` | ^ help: consider adding suffix: `1_i32`
error: default numeric fallback might occur error: default numeric fallback might occur
--> $DIR/default_numeric_fallback.rs:111:36 --> $DIR/default_numeric_fallback.rs:112:36
| |
LL | let _ = GenericStruct { x: 1 }; LL | let _ = GenericStruct { x: 1 };
| ^ help: consider adding suffix: `1_i32` | ^ help: consider adding suffix: `1_i32`
error: default numeric fallback might occur error: default numeric fallback might occur
--> $DIR/default_numeric_fallback.rs:131:23 --> $DIR/default_numeric_fallback.rs:132:23
| |
LL | s.generic_arg(1); LL | s.generic_arg(1);
| ^ help: consider adding suffix: `1_i32` | ^ help: consider adding suffix: `1_i32`

View file

@ -82,5 +82,31 @@ LL | | 42
LL | | }; LL | | };
| |_____^ | |_____^
error: aborting due to 4 previous errors error: this `if` has identical blocks
--> $DIR/if_same_then_else.rs:95:13
|
LL | if true {
| _____________^
LL | | let bar = if true { 42 } else { 43 };
LL | |
LL | | while foo() {
... |
LL | | bar + 1;
LL | | } else {
| |_____^
|
note: same as this
--> $DIR/if_same_then_else.rs:102:12
|
LL | } else {
| ____________^
LL | | //~ ERROR same body as `if` block
LL | | let bar = if true { 42 } else { 43 };
LL | |
... |
LL | | bar + 1;
LL | | }
| |_____^
error: aborting due to 5 previous errors

View file

@ -101,5 +101,25 @@ LL | | Ok("foo")?;
LL | | } LL | | }
| |_____^ | |_____^
error: aborting due to 5 previous errors error: this `if` has identical blocks
--> $DIR/if_same_then_else2.rs:122:20
|
LL | } else if true {
| ____________________^
LL | | let foo = "";
LL | | return Ok(&foo[0..]);
LL | | } else {
| |_____^
|
note: same as this
--> $DIR/if_same_then_else2.rs:125:12
|
LL | } else {
| ____________^
LL | | let foo = "";
LL | | return Ok(&foo[0..]);
LL | | }
| |_____^
error: aborting due to 6 previous errors

View file

@ -4,7 +4,8 @@
dead_code, dead_code,
clippy::no_effect, clippy::no_effect,
clippy::if_same_then_else, clippy::if_same_then_else,
clippy::needless_return clippy::needless_return,
clippy::shared_code_in_if_blocks
)] )]
fn main() { fn main() {

View file

@ -1,5 +1,5 @@
error: this if-then-else expression will always return true error: this if-then-else expression will always return true
--> $DIR/simple.rs:13:5 --> $DIR/simple.rs:14:5
| |
LL | / if x { LL | / if x {
LL | | true LL | | true
@ -11,7 +11,7 @@ LL | | };
= note: `-D clippy::needless-bool` implied by `-D warnings` = note: `-D clippy::needless-bool` implied by `-D warnings`
error: this if-then-else expression will always return false error: this if-then-else expression will always return false
--> $DIR/simple.rs:18:5 --> $DIR/simple.rs:19:5
| |
LL | / if x { LL | / if x {
LL | | false LL | | false
@ -21,7 +21,7 @@ LL | | };
| |_____^ | |_____^
error: this if-then-else expression will always return true error: this if-then-else expression will always return true
--> $DIR/simple.rs:33:5 --> $DIR/simple.rs:34:5
| |
LL | / if x { LL | / if x {
LL | | return true; LL | | return true;
@ -31,7 +31,7 @@ LL | | };
| |_____^ | |_____^
error: this if-then-else expression will always return false error: this if-then-else expression will always return false
--> $DIR/simple.rs:41:5 --> $DIR/simple.rs:42:5
| |
LL | / if x { LL | / if x {
LL | | return false; LL | | return false;

View file

@ -1,7 +1,12 @@
// run-rustfix // run-rustfix
#![allow(unused, clippy::needless_bool)] #![allow(unused)]
#![allow(clippy::if_same_then_else, clippy::single_match)] #![allow(
clippy::if_same_then_else,
clippy::single_match,
clippy::shared_code_in_if_blocks,
clippy::needless_bool
)]
#![warn(clippy::needless_return)] #![warn(clippy::needless_return)]
macro_rules! the_answer { macro_rules! the_answer {

View file

@ -1,7 +1,12 @@
// run-rustfix // run-rustfix
#![allow(unused, clippy::needless_bool)] #![allow(unused)]
#![allow(clippy::if_same_then_else, clippy::single_match)] #![allow(
clippy::if_same_then_else,
clippy::single_match,
clippy::shared_code_in_if_blocks,
clippy::needless_bool
)]
#![warn(clippy::needless_return)] #![warn(clippy::needless_return)]
macro_rules! the_answer { macro_rules! the_answer {

View file

@ -1,5 +1,5 @@
error: unneeded `return` statement error: unneeded `return` statement
--> $DIR/needless_return.rs:18:5 --> $DIR/needless_return.rs:23:5
| |
LL | return true; LL | return true;
| ^^^^^^^^^^^^ help: remove `return`: `true` | ^^^^^^^^^^^^ help: remove `return`: `true`
@ -7,103 +7,103 @@ LL | return true;
= note: `-D clippy::needless-return` implied by `-D warnings` = note: `-D clippy::needless-return` implied by `-D warnings`
error: unneeded `return` statement error: unneeded `return` statement
--> $DIR/needless_return.rs:22:5 --> $DIR/needless_return.rs:27:5
| |
LL | return true; LL | return true;
| ^^^^^^^^^^^^ help: remove `return`: `true` | ^^^^^^^^^^^^ help: remove `return`: `true`
error: unneeded `return` statement error: unneeded `return` statement
--> $DIR/needless_return.rs:27:9 --> $DIR/needless_return.rs:32:9
| |
LL | return true; LL | return true;
| ^^^^^^^^^^^^ help: remove `return`: `true` | ^^^^^^^^^^^^ help: remove `return`: `true`
error: unneeded `return` statement error: unneeded `return` statement
--> $DIR/needless_return.rs:29:9 --> $DIR/needless_return.rs:34:9
| |
LL | return false; LL | return false;
| ^^^^^^^^^^^^^ help: remove `return`: `false` | ^^^^^^^^^^^^^ help: remove `return`: `false`
error: unneeded `return` statement error: unneeded `return` statement
--> $DIR/needless_return.rs:35:17 --> $DIR/needless_return.rs:40:17
| |
LL | true => return false, LL | true => return false,
| ^^^^^^^^^^^^ help: remove `return`: `false` | ^^^^^^^^^^^^ help: remove `return`: `false`
error: unneeded `return` statement error: unneeded `return` statement
--> $DIR/needless_return.rs:37:13 --> $DIR/needless_return.rs:42:13
| |
LL | return true; LL | return true;
| ^^^^^^^^^^^^ help: remove `return`: `true` | ^^^^^^^^^^^^ help: remove `return`: `true`
error: unneeded `return` statement error: unneeded `return` statement
--> $DIR/needless_return.rs:44:9 --> $DIR/needless_return.rs:49:9
| |
LL | return true; LL | return true;
| ^^^^^^^^^^^^ help: remove `return`: `true` | ^^^^^^^^^^^^ help: remove `return`: `true`
error: unneeded `return` statement error: unneeded `return` statement
--> $DIR/needless_return.rs:46:16 --> $DIR/needless_return.rs:51:16
| |
LL | let _ = || return true; LL | let _ = || return true;
| ^^^^^^^^^^^ help: remove `return`: `true` | ^^^^^^^^^^^ help: remove `return`: `true`
error: unneeded `return` statement error: unneeded `return` statement
--> $DIR/needless_return.rs:54:5 --> $DIR/needless_return.rs:59:5
| |
LL | return; LL | return;
| ^^^^^^^ help: remove `return` | ^^^^^^^ help: remove `return`
error: unneeded `return` statement error: unneeded `return` statement
--> $DIR/needless_return.rs:59:9 --> $DIR/needless_return.rs:64:9
| |
LL | return; LL | return;
| ^^^^^^^ help: remove `return` | ^^^^^^^ help: remove `return`
error: unneeded `return` statement error: unneeded `return` statement
--> $DIR/needless_return.rs:61:9 --> $DIR/needless_return.rs:66:9
| |
LL | return; LL | return;
| ^^^^^^^ help: remove `return` | ^^^^^^^ help: remove `return`
error: unneeded `return` statement error: unneeded `return` statement
--> $DIR/needless_return.rs:68:14 --> $DIR/needless_return.rs:73:14
| |
LL | _ => return, LL | _ => return,
| ^^^^^^ help: replace `return` with an empty block: `{}` | ^^^^^^ help: replace `return` with an empty block: `{}`
error: unneeded `return` statement error: unneeded `return` statement
--> $DIR/needless_return.rs:83:9 --> $DIR/needless_return.rs:88:9
| |
LL | return String::from("test"); LL | return String::from("test");
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: remove `return`: `String::from("test")` | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: remove `return`: `String::from("test")`
error: unneeded `return` statement error: unneeded `return` statement
--> $DIR/needless_return.rs:85:9 --> $DIR/needless_return.rs:90:9
| |
LL | return String::new(); LL | return String::new();
| ^^^^^^^^^^^^^^^^^^^^^ help: remove `return`: `String::new()` | ^^^^^^^^^^^^^^^^^^^^^ help: remove `return`: `String::new()`
error: unneeded `return` statement error: unneeded `return` statement
--> $DIR/needless_return.rs:106:32 --> $DIR/needless_return.rs:111:32
| |
LL | bar.unwrap_or_else(|_| return) LL | bar.unwrap_or_else(|_| return)
| ^^^^^^ help: replace `return` with an empty block: `{}` | ^^^^^^ help: replace `return` with an empty block: `{}`
error: unneeded `return` statement error: unneeded `return` statement
--> $DIR/needless_return.rs:111:13 --> $DIR/needless_return.rs:116:13
| |
LL | return; LL | return;
| ^^^^^^^ help: remove `return` | ^^^^^^^ help: remove `return`
error: unneeded `return` statement error: unneeded `return` statement
--> $DIR/needless_return.rs:113:20 --> $DIR/needless_return.rs:118:20
| |
LL | let _ = || return; LL | let _ = || return;
| ^^^^^^ help: replace `return` with an empty block: `{}` | ^^^^^^ help: replace `return` with an empty block: `{}`
error: unneeded `return` statement error: unneeded `return` statement
--> $DIR/needless_return.rs:119:32 --> $DIR/needless_return.rs:124:32
| |
LL | res.unwrap_or_else(|_| return Foo) LL | res.unwrap_or_else(|_| return Foo)
| ^^^^^^^^^^ help: remove `return`: `Foo` | ^^^^^^^^^^ help: remove `return`: `Foo`

View file

@ -190,4 +190,20 @@ fn test_suggestion_with_weird_formatting() {
if x == 17 { b = 1; a = 0x99; } else { a = 0x99; } 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() {} fn main() {}

View file

@ -12,7 +12,7 @@ note: the lint level is defined here
| |
LL | #![deny(clippy::if_same_then_else, clippy::shared_code_in_if_blocks)] 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. = note: The end suggestion probably needs some adjustments to use the expression result correctly
help: consider moving the end statements out like this help: consider moving the end statements out like this
| |
LL | } LL | }
@ -75,7 +75,7 @@ LL | | // I'm expecting a note about this
LL | | } LL | | }
| |_____^ | |_____^
| |
= warning: Some moved values might need to be renamed to avoid wrong references. = warning: Some moved values might need to be renamed to avoid wrong references
help: consider moving the end statements out like this help: consider moving the end statements out like this
| |
LL | } LL | }
@ -91,7 +91,7 @@ LL | | println!("This is the new simple_example: {}", simple_examples);
LL | | } LL | | }
| |_____^ | |_____^
| |
= warning: Some moved values might need to be renamed to avoid wrong references. = warning: Some moved values might need to be renamed to avoid wrong references
help: consider moving the end statements out like this help: consider moving the end statements out like this
| |
LL | } LL | }
@ -106,7 +106,7 @@ LL | / x << 2
LL | | }; LL | | };
| |_____^ | |_____^
| |
= note: The end suggestion probably needs some adjustments to use the expression result correctly. = note: The end suggestion probably needs some adjustments to use the expression result correctly
help: consider moving the end statements out like this help: consider moving the end statements out like this
| |
LL | } LL | }
@ -120,7 +120,7 @@ LL | / x * 4
LL | | } LL | | }
| |_____^ | |_____^
| |
= note: The end suggestion probably needs some adjustments to use the expression result correctly. = note: The end suggestion probably needs some adjustments to use the expression result correctly
help: consider moving the end statements out like this help: consider moving the end statements out like this
| |
LL | } LL | }

View file

@ -25,7 +25,7 @@ LL | | println!("The value y was set to: `{}`", y);
LL | | let _z = y; LL | | let _z = y;
| |___________________^ | |___________________^
| |
= warning: Some moved values might need to be renamed to avoid wrong references. = warning: Some moved values might need to be renamed to avoid wrong references
help: consider moving the start statements out like this help: consider moving the start statements out like this
| |
LL | let y = 9; LL | let y = 9;
@ -55,7 +55,7 @@ LL | | let used_value_name = "Different type";
LL | | println!("Str: {}", used_value_name); LL | | println!("Str: {}", used_value_name);
| |_____________________________________________^ | |_____________________________________________^
| |
= warning: Some moved values might need to be renamed to avoid wrong references. = warning: Some moved values might need to be renamed to avoid wrong references
help: consider moving the start statements out like this help: consider moving the start statements out like this
| |
LL | let used_value_name = "Different type"; LL | let used_value_name = "Different type";
@ -71,7 +71,7 @@ LL | | let can_be_overridden = "Move me";
LL | | println!("I'm also moveable"); LL | | println!("I'm also moveable");
| |______________________________________^ | |______________________________________^
| |
= warning: Some moved values might need to be renamed to avoid wrong references. = warning: Some moved values might need to be renamed to avoid wrong references
help: consider moving the start statements out like this help: consider moving the start statements out like this
| |
LL | let can_be_overridden = "Move me"; LL | let can_be_overridden = "Move me";

View file

@ -47,7 +47,7 @@ LL | / let _overlap_end = r * r * r;
LL | | let z = "end"; LL | | let z = "end";
LL | | } LL | | }
| |_____^ | |_____^
= warning: Some moved values might need to be renamed to avoid wrong references. = warning: Some moved values might need to be renamed to avoid wrong references
help: consider moving the start statements out like this help: consider moving the start statements out like this
| |
LL | let r = 7; LL | let r = 7;
@ -82,7 +82,7 @@ LL | | };
LL | | process_data(pack); LL | | process_data(pack);
LL | | } LL | | }
| |_____^ | |_____^
= warning: Some moved values might need to be renamed to avoid wrong references. = warning: Some moved values might need to be renamed to avoid wrong references
help: consider moving the start statements out like this help: consider moving the start statements out like this
| |
LL | let a = 0xcafe; LL | let a = 0xcafe;
@ -113,7 +113,7 @@ note: and here at the end
LL | / x << 2 LL | / x << 2
LL | | }; LL | | };
| |_____^ | |_____^
= note: The end suggestion probably needs some adjustments to use the expression result correctly. = note: The end suggestion probably needs some adjustments to use the expression result correctly
help: consider moving the start statements out like this help: consider moving the start statements out like this
| |
LL | let _ = 19; LL | let _ = 19;
@ -138,7 +138,7 @@ note: and here at the end
LL | / x * 4 LL | / x * 4
LL | | } LL | | }
| |_____^ | |_____^
= note: The end suggestion probably needs some adjustments to use the expression result correctly. = note: The end suggestion probably needs some adjustments to use the expression result correctly
help: consider moving the start statements out like this help: consider moving the start statements out like this
| |
LL | let _ = 17; LL | let _ = 17;

View file

@ -91,6 +91,14 @@ fn valid_examples() {
let _ = (x, y, z); let _ = (x, y, z);
// I'm so much better than the x == 418 block. Trust me // 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);
}
} }
/// This makes sure that the `if_same_then_else` masks the `shared_code_in_if_blocks` lint /// This makes sure that the `if_same_then_else` masks the `shared_code_in_if_blocks` lint

View file

@ -1,5 +1,5 @@
error: this `if` has identical blocks error: this `if` has identical blocks
--> $DIR/valid_if_blocks.rs:102:15 --> $DIR/valid_if_blocks.rs:110:15
| |
LL | if x == 0 { LL | if x == 0 {
| _______________^ | _______________^
@ -15,7 +15,7 @@ note: the lint level is defined here
LL | #![deny(clippy::if_same_then_else, clippy::shared_code_in_if_blocks)] LL | #![deny(clippy::if_same_then_else, clippy::shared_code_in_if_blocks)]
| ^^^^^^^^^^^^^^^^^^^^^^^^^ | ^^^^^^^^^^^^^^^^^^^^^^^^^
note: same as this note: same as this
--> $DIR/valid_if_blocks.rs:106:12 --> $DIR/valid_if_blocks.rs:114:12
| |
LL | } else { LL | } else {
| ____________^ | ____________^
@ -26,19 +26,19 @@ LL | | }
| |_____^ | |_____^
error: this `if` has identical blocks error: this `if` has identical blocks
--> $DIR/valid_if_blocks.rs:113:23 --> $DIR/valid_if_blocks.rs:121:23
| |
LL | let _ = if x == 6 { 7 } else { 7 }; LL | let _ = if x == 6 { 7 } else { 7 };
| ^^^^^ | ^^^^^
| |
note: same as this note: same as this
--> $DIR/valid_if_blocks.rs:113:34 --> $DIR/valid_if_blocks.rs:121:34
| |
LL | let _ = if x == 6 { 7 } else { 7 }; LL | let _ = if x == 6 { 7 } else { 7 };
| ^^^^^ | ^^^^^
error: this `if` has identical blocks error: this `if` has identical blocks
--> $DIR/valid_if_blocks.rs:119:23 --> $DIR/valid_if_blocks.rs:127:23
| |
LL | } else if x == 68 { LL | } else if x == 68 {
| _______________________^ | _______________________^
@ -51,7 +51,7 @@ LL | | } else {
| |_____^ | |_____^
| |
note: same as this note: same as this
--> $DIR/valid_if_blocks.rs:128:12 --> $DIR/valid_if_blocks.rs:136:12
| |
LL | } else { LL | } else {
| ____________^ | ____________^
@ -64,7 +64,7 @@ LL | | };
| |_____^ | |_____^
error: this `if` has identical blocks error: this `if` has identical blocks
--> $DIR/valid_if_blocks.rs:141:23 --> $DIR/valid_if_blocks.rs:149:23
| |
LL | } else if x == 68 { LL | } else if x == 68 {
| _______________________^ | _______________________^
@ -74,7 +74,7 @@ LL | | } else {
| |_____^ | |_____^
| |
note: same as this note: same as this
--> $DIR/valid_if_blocks.rs:144:12 --> $DIR/valid_if_blocks.rs:152:12
| |
LL | } else { LL | } else {
| ____________^ | ____________^