rust/tests/ui/booleans.rs

154 lines
4.0 KiB
Rust
Raw Normal View History

2018-10-06 11:18:06 -05: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-07-28 10:34:52 -05:00
#![warn(clippy::nonminimal_bool, clippy::logic_bug)]
2017-09-18 05:47:33 -05:00
2018-07-28 10:34:52 -05:00
#[allow(unused, clippy::many_single_char_names)]
2016-03-23 08:50:47 -05:00
fn main() {
let a: bool = unimplemented!();
let b: bool = unimplemented!();
let c: bool = unimplemented!();
let d: bool = unimplemented!();
let e: bool = unimplemented!();
2017-02-08 07:58:07 -06:00
let _ = a && b || a;
let _ = !(a && b);
2017-02-08 07:58:07 -06:00
let _ = !true;
let _ = !false;
let _ = !!a;
let _ = false && a;
let _ = false || a;
// don't lint on cfgs
let _ = cfg!(you_shall_not_not_pass) && a;
let _ = a || !b || !c || !d || !e;
let _ = !(a && b || c);
2017-02-08 07:58:07 -06:00
let _ = !(!a && b);
2016-03-23 08:50:47 -05:00
}
2018-07-28 10:34:52 -05:00
#[allow(unused, clippy::many_single_char_names)]
fn equality_stuff() {
let a: i32 = unimplemented!();
let b: i32 = unimplemented!();
let c: i32 = unimplemented!();
let d: i32 = unimplemented!();
let e: i32 = unimplemented!();
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;
let _ = a > b && a == b;
let _ = a != b || !(a != b || c == d);
}
2018-07-28 10:34:52 -05:00
#[allow(unused, clippy::many_single_char_names)]
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();
let c = false;
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();
}
// 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
// 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,
}
impl<E> CustomResultOk<E> {
2018-12-09 16:26:16 -06:00
pub fn is_ok(&self) -> bool {
true
}
}
impl<E> CustomResultErr<E> {
2018-12-09 16:26:16 -06:00
pub fn is_err(&self) -> bool {
true
}
}
impl<T> CustomSomeSome<T> {
2018-12-09 16:26:16 -06:00
pub fn is_some(&self) -> bool {
true
}
}
impl<T> CustomSomeNone<T> {
2018-12-09 16:26:16 -06:00
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().
2018-12-09 16:26:16 -06:00
if !res.is_ok() {}
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() {}
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() {}
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() {}
}
// 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() {}
let res = Some(1);
2018-12-09 16:26:16 -06:00
if !res.is_some() {}
if !res.is_none() {}
}
2018-07-28 10:34:52 -05:00
#[allow(clippy::neg_cmp_op_on_partial_ord)]
fn dont_warn_for_negated_partial_ord_comparison() {
let a: f64 = unimplemented!();
let b: f64 = unimplemented!();
let _ = !(a < b);
let _ = !(a <= b);
let _ = !(a > b);
let _ = !(a >= b);
}