Skip to content
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

[comparison_chain] #4827 Check core::cmp::Ord is implemented #4842

Merged

Conversation

timbodeit
Copy link
Contributor

Only emit comparison_chain lint, if cmp is actually available on the type being compared. Don't emit lint in cases where only PartialOrd is implemented.

I haven't yet fully understood Adjustments. I would appreciate, if someone could double check whether my usage of expr_ty in clippy_lints/src/comparison_chain.rs:91 is correct or if there are cases where using expr_ty_adjusted would lead to a different result when used with utils::implements_trait.


fixes #4827
changelog: [comparison_chain] Check core::cmp::Ord is implemented


// Check that the type being compared implements `core::cmp::Ord`
let ty = cx.tables.expr_ty(lhs1);
let ord_id = get_trait_def_id(cx, &paths::ORD).unwrap();
Copy link
Contributor

@tesuji tesuji Nov 24, 2019

Choose a reason for hiding this comment

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

Suggested change
let ord_id = get_trait_def_id(cx, &paths::ORD).unwrap();
if let Some(ord_id) = get_trait_def_id(cx, &paths::ORD);

Copy link
Contributor Author

Choose a reason for hiding this comment

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

There is no if_chain here, that would make this syntax work. Using map_or and a default value of false instead.

Only emit lint, if `cmp` is actually available on the type being
compared. Don't emit lint in cases where only `PartialOrd` is
implemented.
@timbodeit timbodeit force-pushed the comparison-chain-false-positive-4827 branch from 3708b8a to fff9a8e Compare November 24, 2019 09:06
@timbodeit
Copy link
Contributor Author

Getting rid of .unwrap() call and rebasing onto master

Copy link
Member

@flip1995 flip1995 left a comment

Choose a reason for hiding this comment

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

Thanks! r=me with rustup

@flip1995 flip1995 added the S-waiting-on-bors Status: The marked PR was approved and is only waiting bors label Nov 25, 2019
@phansch
Copy link
Member

phansch commented Nov 28, 2019

@bors r=flip1995 rollup

@bors
Copy link
Contributor

bors commented Nov 28, 2019

📌 Commit fff9a8e has been approved by flip1995

phansch added a commit to phansch/rust-clippy that referenced this pull request Nov 28, 2019
…itive-4827, r=flip1995

[comparison_chain] rust-lang#4827 Check `core::cmp::Ord` is implemented

Only emit `comparison_chain` lint, if `cmp` is actually available on the type being compared. Don't emit lint in cases where only `PartialOrd` is implemented.

I haven't yet fully understood [Adjustments](https://doc.rust-lang.org/nightly/nightly-rustc/rustc/ty/adjustment/struct.Adjustment.html). I would appreciate, if someone could double check whether my usage of [expr_ty](https://doc.rust-lang.org/nightly/nightly-rustc/rustc/ty/struct.TypeckTables.html#method.expr_ty) in `clippy_lints/src/comparison_chain.rs:91` is correct or if there are cases where using [expr_ty_adjusted](https://doc.rust-lang.org/nightly/nightly-rustc/rustc/ty/struct.TypeckTables.html#method.expr_ty_adjusted) would lead to a different result when used with `utils::implements_trait`.

---

fixes rust-lang#4827
changelog: [comparison_chain] Check `core::cmp::Ord` is implemented
bors added a commit that referenced this pull request Nov 28, 2019
Rollup of 3 pull requests

Successful merges:

 - #4832 (Add some positive examples to lint docs)
 - #4842 ([comparison_chain] #4827 Check `core::cmp::Ord` is implemented)
 - #4847 (fixing a typo)

Failed merges:

changelog: none

r? @ghost
@bors bors merged commit fff9a8e into rust-lang:master Nov 28, 2019
@timbodeit timbodeit deleted the comparison-chain-false-positive-4827 branch November 29, 2019 18:32
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
S-waiting-on-bors Status: The marked PR was approved and is only waiting bors
Projects
None yet
Development

Successfully merging this pull request may close these issues.

clippy::comparison_chain false positive on PartialOrd
5 participants