-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
change to a structure for parsing assert macro
- Loading branch information
1 parent
f2f5ca6
commit ce66869
Showing
7 changed files
with
307 additions
and
102 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,138 @@ | ||
// run-rustfix | ||
|
||
#![warn(clippy::bool_assert_comparison)] | ||
|
||
use std::ops::Not; | ||
|
||
macro_rules! a { | ||
() => { | ||
true | ||
}; | ||
} | ||
macro_rules! b { | ||
() => { | ||
true | ||
}; | ||
} | ||
|
||
// Implements the Not trait but with an output type | ||
// that's not bool. Should not suggest a rewrite | ||
#[derive(Debug, Copy, Clone)] | ||
#[allow(dead_code)] | ||
enum ImplNotTraitWithoutBool { | ||
VariantX(bool), | ||
VariantY(u32), | ||
} | ||
|
||
impl PartialEq<bool> for ImplNotTraitWithoutBool { | ||
fn eq(&self, other: &bool) -> bool { | ||
match *self { | ||
ImplNotTraitWithoutBool::VariantX(b) => b == *other, | ||
_ => false, | ||
} | ||
} | ||
} | ||
|
||
impl Not for ImplNotTraitWithoutBool { | ||
type Output = Self; | ||
|
||
fn not(self) -> Self::Output { | ||
match self { | ||
ImplNotTraitWithoutBool::VariantX(b) => ImplNotTraitWithoutBool::VariantX(!b), | ||
ImplNotTraitWithoutBool::VariantY(0) => ImplNotTraitWithoutBool::VariantY(1), | ||
ImplNotTraitWithoutBool::VariantY(_) => ImplNotTraitWithoutBool::VariantY(0), | ||
} | ||
} | ||
} | ||
|
||
// This type implements the Not trait with an Output of | ||
// type bool. Using assert!(..) must be suggested | ||
#[derive(Debug, Clone, Copy)] | ||
struct ImplNotTraitWithBool; | ||
|
||
impl PartialEq<bool> for ImplNotTraitWithBool { | ||
fn eq(&self, _other: &bool) -> bool { | ||
false | ||
} | ||
} | ||
|
||
impl Not for ImplNotTraitWithBool { | ||
type Output = bool; | ||
|
||
fn not(self) -> Self::Output { | ||
true | ||
} | ||
} | ||
|
||
fn main() { | ||
let a = ImplNotTraitWithoutBool::VariantX(true); | ||
let b = ImplNotTraitWithBool; | ||
|
||
assert_eq!("a".len(), 1); | ||
assert!(!"a".is_empty()); | ||
assert!("".is_empty()); | ||
assert!("".is_empty()); | ||
assert_eq!(a!(), b!()); | ||
assert_eq!(a!(), "".is_empty()); | ||
assert_eq!("".is_empty(), b!()); | ||
assert_eq!(a, true); | ||
assert!(b); | ||
|
||
assert_ne!("a".len(), 1); | ||
assert!("a".is_empty()); | ||
assert!(!"".is_empty()); | ||
assert!(!"".is_empty()); | ||
assert_ne!(a!(), b!()); | ||
assert_ne!(a!(), "".is_empty()); | ||
assert_ne!("".is_empty(), b!()); | ||
assert_ne!(a, true); | ||
assert!(!b); | ||
|
||
debug_assert_eq!("a".len(), 1); | ||
debug_assert!(!"a".is_empty()); | ||
debug_assert!("".is_empty()); | ||
debug_assert!("".is_empty()); | ||
debug_assert_eq!(a!(), b!()); | ||
debug_assert_eq!(a!(), "".is_empty()); | ||
debug_assert_eq!("".is_empty(), b!()); | ||
debug_assert_eq!(a, true); | ||
debug_assert!(b); | ||
|
||
debug_assert_ne!("a".len(), 1); | ||
debug_assert!("a".is_empty()); | ||
debug_assert!(!"".is_empty()); | ||
debug_assert!(!"".is_empty()); | ||
debug_assert_ne!(a!(), b!()); | ||
debug_assert_ne!(a!(), "".is_empty()); | ||
debug_assert_ne!("".is_empty(), b!()); | ||
debug_assert_ne!(a, true); | ||
debug_assert!(!b); | ||
|
||
// assert with error messages | ||
assert_eq!("a".len(), 1, "tadam {}", 1); | ||
assert_eq!("a".len(), 1, "tadam {}", true); | ||
assert!(!"a".is_empty(), "tadam {}", 1); | ||
assert!(!"a".is_empty(), "tadam {}", true); | ||
assert!(!"a".is_empty(), "tadam {}", true); | ||
assert_eq!(a, true, "tadam {}", false); | ||
assert!("a".is_empty(), "tadam {} {}", false, 6); | ||
|
||
debug_assert_eq!("a".len(), 1, "tadam {}", 1); | ||
debug_assert_eq!("a".len(), 1, "tadam {}", true); | ||
debug_assert!(!"a".is_empty(), "tadam {}", 1); | ||
debug_assert!(!"a".is_empty(), "tadam {}", true); | ||
debug_assert!(!"a".is_empty(), "tadam {}", true); | ||
debug_assert_eq!(a, true, "tadam {}", false); | ||
debug_assert!("a".is_empty(), "tadam {} {}", false, "b"); | ||
|
||
// Without ; at the end | ||
{ | ||
debug_assert!(!"a".is_empty(), "tadam {}", true); | ||
}; | ||
{ | ||
assert_eq!("a".len(), 1, "tadam {}", 1) | ||
}; | ||
{ | ||
assert_ne!("a".len(), 1, "tadam {}", true) | ||
}; | ||
} |
Oops, something went wrong.