2023-06-10 06:43:30 -05:00
|
|
|
#![allow(unused, clippy::diverging_sub_expression, clippy::needless_if)]
|
2020-01-13 16:41:11 -06:00
|
|
|
#![warn(clippy::nonminimal_bool)]
|
2017-09-18 05:47:33 -05:00
|
|
|
|
2017-11-07 15:43:24 -06:00
|
|
|
fn methods_with_negation() {
|
|
|
|
let a: Option<i32> = unimplemented!();
|
|
|
|
let b: Result<i32, i32> = unimplemented!();
|
|
|
|
let _ = a.is_some();
|
|
|
|
let _ = !a.is_some();
|
|
|
|
let _ = a.is_none();
|
|
|
|
let _ = !a.is_none();
|
|
|
|
let _ = b.is_err();
|
|
|
|
let _ = !b.is_err();
|
|
|
|
let _ = b.is_ok();
|
|
|
|
let _ = !b.is_ok();
|
2017-11-14 10:07:04 -06:00
|
|
|
let c = false;
|
|
|
|
let _ = !(a.is_some() && !c);
|
2020-01-13 16:08:45 -06:00
|
|
|
let _ = !(a.is_some() || !c);
|
2017-11-17 15:52:11 -06:00
|
|
|
let _ = !(!c ^ c) || !a.is_some();
|
2017-11-19 03:07:50 -06:00
|
|
|
let _ = (!c ^ c) || !a.is_some();
|
|
|
|
let _ = !c ^ c || !a.is_some();
|
2017-11-07 15:43:24 -06:00
|
|
|
}
|
2018-04-07 05:52:18 -05:00
|
|
|
|
2018-11-21 21:40:09 -06:00
|
|
|
// Simplified versions of https://github.com/rust-lang/rust-clippy/issues/2638
|
2018-07-28 10:34:52 -05:00
|
|
|
// clippy::nonminimal_bool should only check the built-in Result and Some type, not
|
2018-04-07 05:52:18 -05:00
|
|
|
// any other types like the following.
|
2018-12-09 16:26:16 -06:00
|
|
|
enum CustomResultOk<E> {
|
|
|
|
Ok,
|
|
|
|
Err(E),
|
|
|
|
}
|
|
|
|
enum CustomResultErr<E> {
|
|
|
|
Ok,
|
|
|
|
Err(E),
|
|
|
|
}
|
|
|
|
enum CustomSomeSome<T> {
|
|
|
|
Some(T),
|
|
|
|
None,
|
|
|
|
}
|
|
|
|
enum CustomSomeNone<T> {
|
|
|
|
Some(T),
|
|
|
|
None,
|
|
|
|
}
|
2018-04-07 05:52:18 -05:00
|
|
|
|
|
|
|
impl<E> CustomResultOk<E> {
|
2018-12-09 16:26:16 -06:00
|
|
|
pub fn is_ok(&self) -> bool {
|
|
|
|
true
|
|
|
|
}
|
2018-04-07 05:52:18 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
impl<E> CustomResultErr<E> {
|
2018-12-09 16:26:16 -06:00
|
|
|
pub fn is_err(&self) -> bool {
|
|
|
|
true
|
|
|
|
}
|
2018-04-07 05:52:18 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
impl<T> CustomSomeSome<T> {
|
2018-12-09 16:26:16 -06:00
|
|
|
pub fn is_some(&self) -> bool {
|
|
|
|
true
|
|
|
|
}
|
2018-04-07 05:52:18 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
impl<T> CustomSomeNone<T> {
|
2018-12-09 16:26:16 -06:00
|
|
|
pub fn is_none(&self) -> bool {
|
|
|
|
true
|
|
|
|
}
|
2018-04-07 05:52:18 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
fn dont_warn_for_custom_methods_with_negation() {
|
|
|
|
let res = CustomResultOk::Err("Error");
|
|
|
|
// Should not warn and suggest 'is_err()' because the type does not
|
|
|
|
// implement is_err().
|
2018-12-09 16:26:16 -06:00
|
|
|
if !res.is_ok() {}
|
2018-04-07 05:52:18 -05:00
|
|
|
|
|
|
|
let res = CustomResultErr::Err("Error");
|
|
|
|
// Should not warn and suggest 'is_ok()' because the type does not
|
|
|
|
// implement is_ok().
|
2018-12-09 16:26:16 -06:00
|
|
|
if !res.is_err() {}
|
2018-04-07 05:52:18 -05:00
|
|
|
|
|
|
|
let res = CustomSomeSome::Some("thing");
|
|
|
|
// Should not warn and suggest 'is_none()' because the type does not
|
|
|
|
// implement is_none().
|
2018-12-09 16:26:16 -06:00
|
|
|
if !res.is_some() {}
|
2018-04-07 05:52:18 -05:00
|
|
|
|
|
|
|
let res = CustomSomeNone::Some("thing");
|
|
|
|
// Should not warn and suggest 'is_some()' because the type does not
|
|
|
|
// implement is_some().
|
2018-12-09 16:26:16 -06:00
|
|
|
if !res.is_none() {}
|
2018-04-07 05:52:18 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// Only Built-in Result and Some types should suggest the negated alternative
|
|
|
|
fn warn_for_built_in_methods_with_negation() {
|
|
|
|
let res: Result<usize, usize> = Ok(1);
|
2018-12-09 16:26:16 -06:00
|
|
|
if !res.is_ok() {}
|
|
|
|
if !res.is_err() {}
|
2018-04-07 05:52:18 -05:00
|
|
|
|
|
|
|
let res = Some(1);
|
2018-12-09 16:26:16 -06:00
|
|
|
if !res.is_some() {}
|
|
|
|
if !res.is_none() {}
|
2018-04-07 05:52:18 -05:00
|
|
|
}
|
2018-06-03 11:46:11 -05:00
|
|
|
|
2018-07-28 10:34:52 -05:00
|
|
|
#[allow(clippy::neg_cmp_op_on_partial_ord)]
|
2018-06-29 09:55:26 -05:00
|
|
|
fn dont_warn_for_negated_partial_ord_comparison() {
|
2018-06-03 11:46:11 -05:00
|
|
|
let a: f64 = unimplemented!();
|
|
|
|
let b: f64 = unimplemented!();
|
|
|
|
let _ = !(a < b);
|
|
|
|
let _ = !(a <= b);
|
|
|
|
let _ = !(a > b);
|
|
|
|
let _ = !(a >= b);
|
|
|
|
}
|
2019-09-24 01:06:58 -05:00
|
|
|
|
2024-04-03 03:10:00 -05:00
|
|
|
fn issue_12625() {
|
|
|
|
let a = 0;
|
|
|
|
let b = 0;
|
|
|
|
if !(a as u64 >= b) {} //~ ERROR: this boolean expression can be simplified
|
|
|
|
if !((a as u64) >= b) {} //~ ERROR: this boolean expression can be simplified
|
|
|
|
if !(a as u64 <= b) {} //~ ERROR: this boolean expression can be simplified
|
|
|
|
}
|
|
|
|
|
2024-09-23 12:28:02 -05:00
|
|
|
fn issue_13436() {
|
|
|
|
fn not_zero(x: i32) -> bool {
|
|
|
|
x != 0
|
|
|
|
}
|
|
|
|
|
|
|
|
let opt = Some(500);
|
|
|
|
_ = opt.is_some_and(|x| x < 1000);
|
|
|
|
_ = opt.is_some_and(|x| x <= 1000);
|
|
|
|
_ = opt.is_some_and(|x| x > 1000);
|
|
|
|
_ = opt.is_some_and(|x| x >= 1000);
|
|
|
|
_ = opt.is_some_and(|x| x == 1000);
|
|
|
|
_ = opt.is_some_and(|x| x != 1000);
|
|
|
|
_ = opt.is_some_and(not_zero);
|
|
|
|
_ = !opt.is_some_and(|x| x < 1000); //~ ERROR: this boolean expression can be simplified
|
|
|
|
_ = !opt.is_some_and(|x| x <= 1000); //~ ERROR: this boolean expression can be simplified
|
|
|
|
_ = !opt.is_some_and(|x| x > 1000); //~ ERROR: this boolean expression can be simplified
|
|
|
|
_ = !opt.is_some_and(|x| x >= 1000); //~ ERROR: this boolean expression can be simplified
|
|
|
|
_ = !opt.is_some_and(|x| x == 1000); //~ ERROR: this boolean expression can be simplified
|
|
|
|
_ = !opt.is_some_and(|x| x != 1000); //~ ERROR: this boolean expression can be simplified
|
|
|
|
_ = !opt.is_some_and(not_zero);
|
|
|
|
_ = opt.is_none_or(|x| x < 1000);
|
|
|
|
_ = opt.is_none_or(|x| x <= 1000);
|
|
|
|
_ = opt.is_none_or(|x| x > 1000);
|
|
|
|
_ = opt.is_none_or(|x| x >= 1000);
|
|
|
|
_ = opt.is_none_or(|x| x == 1000);
|
|
|
|
_ = opt.is_none_or(|x| x != 1000);
|
|
|
|
_ = opt.is_none_or(not_zero);
|
|
|
|
_ = !opt.is_none_or(|x| x < 1000); //~ ERROR: this boolean expression can be simplified
|
|
|
|
_ = !opt.is_none_or(|x| x <= 1000); //~ ERROR: this boolean expression can be simplified
|
|
|
|
_ = !opt.is_none_or(|x| x > 1000); //~ ERROR: this boolean expression can be simplified
|
|
|
|
_ = !opt.is_none_or(|x| x >= 1000); //~ ERROR: this boolean expression can be simplified
|
|
|
|
_ = !opt.is_none_or(|x| x == 1000); //~ ERROR: this boolean expression can be simplified
|
|
|
|
_ = !opt.is_none_or(|x| x != 1000); //~ ERROR: this boolean expression can be simplified
|
|
|
|
_ = !opt.is_none_or(not_zero);
|
|
|
|
|
|
|
|
let opt = Some(true);
|
|
|
|
_ = opt.is_some_and(|x| x);
|
|
|
|
_ = opt.is_some_and(|x| !x);
|
|
|
|
_ = !opt.is_some_and(|x| x);
|
|
|
|
_ = !opt.is_some_and(|x| !x); //~ ERROR: this boolean expression can be simplified
|
|
|
|
_ = opt.is_none_or(|x| x);
|
|
|
|
_ = opt.is_none_or(|x| !x);
|
|
|
|
_ = !opt.is_none_or(|x| x);
|
|
|
|
_ = !opt.is_none_or(|x| !x); //~ ERROR: this boolean expression can be simplified
|
|
|
|
|
|
|
|
let opt: Option<Result<i32, i32>> = Some(Ok(123));
|
|
|
|
_ = opt.is_some_and(|x| x.is_ok());
|
|
|
|
_ = opt.is_some_and(|x| x.is_err());
|
|
|
|
_ = opt.is_none_or(|x| x.is_ok());
|
|
|
|
_ = opt.is_none_or(|x| x.is_err());
|
|
|
|
_ = !opt.is_some_and(|x| x.is_ok()); //~ ERROR: this boolean expression can be simplified
|
|
|
|
_ = !opt.is_some_and(|x| x.is_err()); //~ ERROR: this boolean expression can be simplified
|
|
|
|
_ = !opt.is_none_or(|x| x.is_ok()); //~ ERROR: this boolean expression can be simplified
|
|
|
|
_ = !opt.is_none_or(|x| x.is_err()); //~ ERROR: this boolean expression can be simplified
|
|
|
|
|
|
|
|
#[clippy::msrv = "1.81"]
|
|
|
|
fn before_stabilization() {
|
|
|
|
let opt = Some(500);
|
|
|
|
_ = !opt.is_some_and(|x| x < 1000);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-01-13 16:41:11 -06:00
|
|
|
fn main() {}
|