diff options
author | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-05-30 03:57:31 +0000 |
---|---|---|
committer | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-05-30 03:57:31 +0000 |
commit | dc0db358abe19481e475e10c32149b53370f1a1c (patch) | |
tree | ab8ce99c4b255ce46f99ef402c27916055b899ee /src/tools/clippy/tests/ui/needless_if.rs | |
parent | Releasing progress-linux version 1.71.1+dfsg1-2~progress7.99u1. (diff) | |
download | rustc-dc0db358abe19481e475e10c32149b53370f1a1c.tar.xz rustc-dc0db358abe19481e475e10c32149b53370f1a1c.zip |
Merging upstream version 1.72.1+dfsg1.
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'src/tools/clippy/tests/ui/needless_if.rs')
-rw-r--r-- | src/tools/clippy/tests/ui/needless_if.rs | 94 |
1 files changed, 94 insertions, 0 deletions
diff --git a/src/tools/clippy/tests/ui/needless_if.rs b/src/tools/clippy/tests/ui/needless_if.rs new file mode 100644 index 000000000..eb28ce73b --- /dev/null +++ b/src/tools/clippy/tests/ui/needless_if.rs @@ -0,0 +1,94 @@ +//@run-rustfix +//@aux-build:proc_macros.rs:proc-macro +#![feature(let_chains)] +#![allow( + clippy::blocks_in_if_conditions, + clippy::if_same_then_else, + clippy::ifs_same_cond, + clippy::let_unit_value, + clippy::needless_else, + clippy::no_effect, + clippy::nonminimal_bool, + clippy::short_circuit_statement, + clippy::unnecessary_operation, + unused +)] +#![warn(clippy::needless_if)] + +extern crate proc_macros; +use proc_macros::external; +use proc_macros::with_span; + +fn maybe_side_effect() -> bool { + true +} + +fn main() { + // Lint + if (true) {} + // Do not remove the condition + if maybe_side_effect() {} + // Do not lint + if (true) { + } else { + } + if { + return; + } {} + // Do not lint if `else if` is present + if (true) { + } else if (true) { + } + // Do not lint `if let` or let chains + if let true = true {} + if let true = true && true {} + if true && let true = true {} + // Can lint nested `if let`s + if { + if let true = true && true { true } else { false } + } && true + {} + external! { if (true) {} } + with_span! { + span + if (true) {} + } + + if true { + // comment + } + + if true { + #[cfg(any())] + foo; + } + + macro_rules! empty_expansion { + () => {}; + } + + if true { + empty_expansion!(); + } + + macro_rules! empty_repetition { + ($($t:tt)*) => { + if true { + $($t)* + } + } + } + + empty_repetition!(); + + // Must be placed into an expression context to not be interpreted as a block + if { maybe_side_effect() } {} + // Would be a block followed by `&&true` - a double reference to `true` + if { maybe_side_effect() } && true {} + + // Don't leave trailing attributes + #[allow(unused)] + if true {} + + let () = if maybe_side_effect() {}; +} |