Skip to content

Conversation

0ndorio
Copy link
Contributor

@0ndorio 0ndorio commented Jun 1, 2018

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.

@0ndorio 0ndorio force-pushed the lint/cmp_operators_on_partial_cmp branch from 5bcd6e0 to 4b4d617 Compare June 1, 2018 10:27
Copy link
Contributor

@oli-obk oli-obk left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

cx.span_lint(
NEG_CMP_OP_ON_PARTIAL_ORD,
expr.span,
"The use of negated comparision operators on partially orded\
Copy link
Contributor

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\
Copy link
Contributor

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

Copy link
Contributor Author

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?

Copy link
Contributor

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,
Copy link
Contributor

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

@0ndorio 0ndorio changed the title [WIP] Added lint to avoid negated comparisions on partially ordered types. (fixes #2626) Added lint to avoid negated comparisions on partially ordered types. (fixes #2626) Jun 1, 2018
Copy link
Contributor

@oli-obk oli-obk left a 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?

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) {
Copy link
Contributor

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, &[]))

}


fn implements_ord<'a, 'tcx>(cx: &'a LateContext<'a, 'tcx>, expr: &Expr) -> Option<bool> {
Copy link
Contributor

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)

None
};
get_trait_def_id(cx, &paths::ORD)
.map(|id| implements_trait(cx, ty, id, &[]))
Copy link
Contributor

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|....

Copy link
Contributor Author

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.

@0ndorio 0ndorio force-pushed the lint/cmp_operators_on_partial_cmp branch from 2cc6cb1 to b803f85 Compare June 3, 2018 20:48
@0ndorio
Copy link
Contributor Author

0ndorio commented Jun 3, 2018

Simplified the map().unwrap() chain as you mentioned, added the missing test calls and rebased everything on top of latest master.

@0ndorio 0ndorio force-pushed the lint/cmp_operators_on_partial_cmp branch from b803f85 to 28f735b Compare June 3, 2018 20:56
@oli-obk
Copy link
Contributor

oli-obk commented Jun 3, 2018

Thanks! Looks great now

@oli-obk oli-obk merged commit 3d7cdd4 into rust-lang:master Jun 3, 2018
@0ndorio 0ndorio deleted the lint/cmp_operators_on_partial_cmp branch June 20, 2018 07:46
@0ndorio 0ndorio restored the lint/cmp_operators_on_partial_cmp branch October 15, 2018 15:16
@0ndorio 0ndorio deleted the lint/cmp_operators_on_partial_cmp branch October 2, 2019 11:52
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants