2018-10-06 18:18:06 +02:00
|
|
|
// Copyright 2014-2018 The Rust Project Developers. See the COPYRIGHT
|
|
|
|
// file at the top-level directory of this distribution.
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
|
|
|
|
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
|
|
|
|
// option. This file may not be copied, modified, or distributed
|
|
|
|
// except according to those terms.
|
|
|
|
|
|
|
|
|
2018-10-11 12:16:22 +02:00
|
|
|
|
2017-09-18 12:47:33 +02:00
|
|
|
|
2018-07-28 17:34:52 +02:00
|
|
|
#![warn(clippy::nonminimal_bool, clippy::logic_bug)]
|
2017-09-18 12:47:33 +02:00
|
|
|
|
2018-07-28 17:34:52 +02:00
|
|
|
#[allow(unused, clippy::many_single_char_names)]
|
2016-03-23 14:50:47 +01:00
|
|
|
fn main() {
|
|
|
|
let a: bool = unimplemented!();
|
|
|
|
let b: bool = unimplemented!();
|
2016-03-24 10:54:48 +01:00
|
|
|
let c: bool = unimplemented!();
|
2016-03-24 15:37:17 +01:00
|
|
|
let d: bool = unimplemented!();
|
|
|
|
let e: bool = unimplemented!();
|
2017-02-08 14:58:07 +01:00
|
|
|
let _ = a && b || a;
|
2016-03-24 10:54:48 +01:00
|
|
|
let _ = !(a && b);
|
2017-02-08 14:58:07 +01:00
|
|
|
let _ = !true;
|
|
|
|
let _ = !false;
|
|
|
|
let _ = !!a;
|
|
|
|
let _ = false && a;
|
|
|
|
let _ = false || a;
|
2016-03-24 09:37:16 +01:00
|
|
|
// don't lint on cfgs
|
|
|
|
let _ = cfg!(you_shall_not_not_pass) && a;
|
2016-03-24 15:37:17 +01:00
|
|
|
let _ = a || !b || !c || !d || !e;
|
2016-03-24 10:54:48 +01:00
|
|
|
let _ = !(a && b || c);
|
2017-02-08 14:58:07 +01:00
|
|
|
let _ = !(!a && b);
|
2016-03-23 14:50:47 +01:00
|
|
|
}
|
2016-03-24 15:44:08 +01:00
|
|
|
|
2018-07-28 17:34:52 +02:00
|
|
|
#[allow(unused, clippy::many_single_char_names)]
|
2016-03-24 15:44:08 +01:00
|
|
|
fn equality_stuff() {
|
|
|
|
let a: i32 = unimplemented!();
|
|
|
|
let b: i32 = unimplemented!();
|
|
|
|
let c: i32 = unimplemented!();
|
|
|
|
let d: i32 = unimplemented!();
|
|
|
|
let e: i32 = unimplemented!();
|
2016-06-07 18:32:26 +02:00
|
|
|
let _ = a == b && a != b;
|
|
|
|
let _ = a == b && c == 5 && a == b;
|
|
|
|
let _ = a == b && c == 5 && b == a;
|
|
|
|
let _ = a < b && a >= b;
|
|
|
|
let _ = a > b && a <= b;
|
2016-03-24 15:44:08 +01:00
|
|
|
let _ = a > b && a == b;
|
2016-06-07 18:32:26 +02:00
|
|
|
let _ = a != b || !(a != b || c == d);
|
2016-03-24 15:44:08 +01:00
|
|
|
}
|
2017-11-07 22:43:24 +01:00
|
|
|
|
2018-07-28 17:34:52 +02:00
|
|
|
#[allow(unused, clippy::many_single_char_names)]
|
2017-11-07 22:43:24 +01: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 17:07:04 +01:00
|
|
|
let c = false;
|
|
|
|
let _ = !(a.is_some() && !c);
|
2017-11-17 22:52:11 +01:00
|
|
|
let _ = !(!c ^ c) || !a.is_some();
|
2017-11-19 10:07:50 +01:00
|
|
|
let _ = (!c ^ c) || !a.is_some();
|
|
|
|
let _ = !c ^ c || !a.is_some();
|
2017-11-07 22:43:24 +01:00
|
|
|
}
|
2018-04-07 12:52:18 +02:00
|
|
|
|
|
|
|
// Simplified versions of https://github.com/rust-lang-nursery/rust-clippy/issues/2638
|
2018-07-28 17:34:52 +02:00
|
|
|
// clippy::nonminimal_bool should only check the built-in Result and Some type, not
|
2018-04-07 12:52:18 +02:00
|
|
|
// any other types like the following.
|
|
|
|
enum CustomResultOk<E> { Ok, Err(E) }
|
|
|
|
enum CustomResultErr<E> { Ok, Err(E) }
|
|
|
|
enum CustomSomeSome<T> { Some(T), None }
|
|
|
|
enum CustomSomeNone<T> { Some(T), None }
|
|
|
|
|
|
|
|
impl<E> CustomResultOk<E> {
|
|
|
|
pub fn is_ok(&self) -> bool { true }
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<E> CustomResultErr<E> {
|
|
|
|
pub fn is_err(&self) -> bool { true }
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<T> CustomSomeSome<T> {
|
|
|
|
pub fn is_some(&self) -> bool { true }
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<T> CustomSomeNone<T> {
|
|
|
|
pub fn is_none(&self) -> bool { true }
|
|
|
|
}
|
|
|
|
|
|
|
|
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().
|
|
|
|
if !res.is_ok() { }
|
|
|
|
|
|
|
|
let res = CustomResultErr::Err("Error");
|
|
|
|
// Should not warn and suggest 'is_ok()' because the type does not
|
|
|
|
// implement is_ok().
|
|
|
|
if !res.is_err() { }
|
|
|
|
|
|
|
|
let res = CustomSomeSome::Some("thing");
|
|
|
|
// Should not warn and suggest 'is_none()' because the type does not
|
|
|
|
// implement is_none().
|
|
|
|
if !res.is_some() { }
|
|
|
|
|
|
|
|
let res = CustomSomeNone::Some("thing");
|
|
|
|
// Should not warn and suggest 'is_some()' because the type does not
|
|
|
|
// implement is_some().
|
|
|
|
if !res.is_none() { }
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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);
|
|
|
|
if !res.is_ok() { }
|
|
|
|
if !res.is_err() { }
|
|
|
|
|
|
|
|
let res = Some(1);
|
|
|
|
if !res.is_some() { }
|
|
|
|
if !res.is_none() { }
|
|
|
|
}
|
2018-06-03 18:46:11 +02:00
|
|
|
|
2018-07-28 17:34:52 +02:00
|
|
|
#[allow(clippy::neg_cmp_op_on_partial_ord)]
|
2018-06-29 16:55:26 +02:00
|
|
|
fn dont_warn_for_negated_partial_ord_comparison() {
|
2018-06-03 18:46:11 +02:00
|
|
|
let a: f64 = unimplemented!();
|
|
|
|
let b: f64 = unimplemented!();
|
|
|
|
let _ = !(a < b);
|
|
|
|
let _ = !(a <= b);
|
|
|
|
let _ = !(a > b);
|
|
|
|
let _ = !(a >= b);
|
|
|
|
}
|