-
Notifications
You must be signed in to change notification settings - Fork 1.8k
Added lint to avoid negated comparisions on partially ordered types. (fixes #2626) #2827
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Added lint to avoid negated comparisions on partially ordered types. (fixes #2626) #2827
Conversation
5bcd6e0
to
4b4d617
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If the lhs
does not implement Ord
, then we should bail out. Relevant code points are
and
cx.span_lint( | ||
NEG_CMP_OP_ON_PARTIAL_ORD, | ||
expr.span, | ||
"The use of negated comparision operators on partially orded\ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
orded
expr.span, | ||
"The use of negated comparision operators on partially orded\ | ||
types produces code that is hard to read and refactor. Please\ | ||
consider to use the partial_cmp() instead, to make it clear\ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
"the partial_cmp()" should be just partial_cmp
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Just partial_cmp or or also surrounded with backticks?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
backticks are always nice
|
||
let _not_less = match a_value.partial_cmp(&another_value) { | ||
None | Some(Ordering::Greater) | Some(Ordering::Equal) => true, | ||
_ => false, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
weird indent and/or tabs instead of spaces
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you add a test ensuring that nonminimal_bool
doesn't trigger for float comparisons anymore?
clippy_lints/src/booleans.rs
Outdated
fn implements_ord<'a, 'tcx>(cx: &'a LateContext<'a, 'tcx>, expr: &Expr) -> Option<bool> { | ||
let ty = cx.tables.expr_ty(expr); | ||
|
||
return if let Some(id) = get_trait_def_id(cx, &paths::ORD) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this can be written as get_trait_def_id(cx, &paths::ORD).map_or(false, implements_trait(cx, ty, id, &[]))
clippy_lints/src/booleans.rs
Outdated
} | ||
|
||
|
||
fn implements_ord<'a, 'tcx>(cx: &'a LateContext<'a, 'tcx>, expr: &Expr) -> Option<bool> { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think this should just return bool
and false
if the trait cannot be found (that would only happen in libcore
to best of my knowledge)
clippy_lints/src/booleans.rs
Outdated
None | ||
}; | ||
get_trait_def_id(cx, &paths::ORD) | ||
.map(|id| implements_trait(cx, ty, id, &[])) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
heads up: miri dogfood will tell you to turn this into map_or(false, |id|....
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is this a hint miri directly outputs if you run it on this repo? I spent a few minutes to check if I can get it running but even if I use the intermediate xargo step it seems like I miss something as miri still fails with no mir for std::env::current_dir
for cargo +nightly miri test
.
…artially orded types.
2cc6cb1
to
b803f85
Compare
Simplified the map().unwrap() chain as you mentioned, added the missing test calls and rebased everything on top of latest master. |
b803f85
to
28f735b
Compare
Thanks! Looks great now |
Added a lint to warn about the usage of negated comparisons on partial ordered types as they always implicitly include the incomparable case (based on issue #2626).
Even if the lint itself seems to work, this isn't done yet as the
nonminimal_bool
lint still triggers on these cases. I am still a bit clueless which is best spot to inject the necessary checks into it so if anyone has a hint I would be glad to fix that.