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

[WIP] Add new lint for xor-ing when pow was probably intended #6182

Closed
wants to merge 5 commits into from

Conversation

cgm616
Copy link
Contributor

@cgm616 cgm616 commented Oct 16, 2020

This is simply a resurrection of #4541, which is inactive. It looks like everything is good to go, pending review.

Closes #4205.


changelog: new lint: [xor_used_as_pow]

@rust-highfive
Copy link

r? @ebroto

(rust_highfive has picked a reviewer for you, use r? to override)

@rust-highfive rust-highfive added the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties label Oct 16, 2020
@cgm616
Copy link
Contributor Author

cgm616 commented Oct 16, 2020

The pr should be totally updated, now---everything compiles in the lint itself, but I still can't compile clippy because of master branch problems.

@cgm616 cgm616 marked this pull request as ready for review October 16, 2020 19:33
@ebroto
Copy link
Member

ebroto commented Oct 16, 2020

Because of compilation problems with the master branch of clippy, I haven't been able to test this

@cgm616 you can try rebasing on master, the problem should be fixed now (#6184)

@cgm616
Copy link
Contributor Author

cgm616 commented Oct 16, 2020

Looks to be all set now!

clippy_lints/src/xor_used_as_pow.rs Outdated Show resolved Hide resolved
clippy_lints/src/xor_used_as_pow.rs Show resolved Hide resolved
clippy_lints/src/xor_used_as_pow.rs Outdated Show resolved Hide resolved
src/lintlist/mod.rs Outdated Show resolved Hide resolved
clippy_lints/src/xor_used_as_pow.rs Outdated Show resolved Hide resolved
clippy_lints/src/xor_used_as_pow.rs Outdated Show resolved Hide resolved
tests/ui/xor_used_as_pow.rs Show resolved Hide resolved
tests/ui/xor_used_as_pow.rs Show resolved Hide resolved
clippy_lints/src/xor_used_as_pow.rs Outdated Show resolved Hide resolved
clippy_lints/src/xor_used_as_pow.rs Outdated Show resolved Hide resolved
@ebroto ebroto added S-waiting-on-author Status: This is awaiting some action from the author. (Use `@rustbot ready` to update this status) and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties labels Oct 18, 2020
@cgm616 cgm616 force-pushed the xor-as-pow branch 2 times, most recently from f9e21a1 to 03ca52e Compare October 22, 2020 04:08
@cgm616
Copy link
Contributor Author

cgm616 commented Oct 22, 2020

I'm unsure how to fix the failing test.... it seems like it has something to do with clippy not fixing all of the errors in the uitest, so the .fixed file still doesn't compile without errors/warnings.

@cgm616 cgm616 requested a review from ebroto October 24, 2020 16:06
@ebroto ebroto added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties and removed S-waiting-on-author Status: This is awaiting some action from the author. (Use `@rustbot ready` to update this status) labels Oct 25, 2020
@bors
Copy link
Collaborator

bors commented Oct 25, 2020

☔ The latest upstream changes (presumably #6109) made this pull request unmergeable. Please resolve the merge conflicts.

Note that reviewers usually do not review pull requests until merge conflicts are resolved! Once you resolve the conflicts, you should change the labels applied by bors to indicate that your PR is ready for review. Post this as a comment to change the labels:

@rustbot modify labels: +S-waiting-on-review -S-waiting-on-author

@cgm616
Copy link
Contributor Author

cgm616 commented Oct 25, 2020

@rustbot modify labels: +S-waiting-on-review -S-waiting-on-author

Copy link
Member

@ebroto ebroto left a comment

Choose a reason for hiding this comment

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

As a side note, I realized we could improve the lint further, but I think it would be better to leave these improvements for follow-up PRs. I will open the issues after we merge this PR.

  • We could give MachineApplicable suggestions for the .pow() case, but would have to handle potential overflows.
  • We could target EXPR ^ EXPR where any of the sides is const-foldable, not only literals. This may require enhancing the consts module.

}
}

fn unwrap_dec_int_literal(cx: &LateContext<'_>, lit: &Lit) -> Option<(u128, LitIntType)> {
Copy link
Member

Choose a reason for hiding this comment

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

It seems that we are not using the second element of the returned tuple

Comment on lines +7 to +22
enum E {
First = 1 ^ 8,
Second = 2 ^ 8,
Third = 3 ^ 8,
Tenth = 10 ^ 8,
}

fn main() {
// These should succeed:
let _ = 9 ^ 3; // lhs other than 2 or 10
let _ = 0x02 ^ 6; // lhs not decimal
let _ = 2 ^ 0x10; // rhs hexadecimal
let _ = 10 ^ 0b0101; // rhs binary
let _ = 2 ^ 0o1; // rhs octal
let _ = 10 ^ -18; // negative rhs
let _ = 2 ^ 0; // zero rhs
Copy link
Member

Choose a reason for hiding this comment

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

We can remove the cases that are already tested in the non-fixable file.

{
let x = 15;
let _ = 10 ^ x;
}
Copy link
Member

Choose a reason for hiding this comment

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

We're missing two tests:

  • The RHS is a const
  • The RHS is an immutable binding initialized with a const

}
}

fn report_with_ident(cx: &LateContext<'_>, lhs: u128, rhs: &QPath<'_>, span: Span) {
Copy link
Member

Choose a reason for hiding this comment

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

In these cases we're not handling the overflow.

let x = 42;
let _ = 2 ^ x;

The suggestion would result in code that does not compile.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I was thinking about this while writing the relevant portions. In order to figure out if this overflows, I'll need to get the literal from the definition (or const or const-binding). Is there a utility for this in clippy already? Particularly, someone might have something like this:

const A: u32 = 42;
const B: u32 = A;

fn main() {
    let c = B;
    let _ = 2 ^ c;
}

It seems like I'd need to iterate over the chain of definitions and grab the one that ends in a literal.

Copy link
Member

Choose a reason for hiding this comment

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

If I'm not mistaken, there is nothing like this in utils.

I remember doing something similar for the internal lint MATCH_TYPE_ON_DIAGNOSTIC_ITEM, in the function path_to_matched_type. I think that could help.

@ebroto ebroto removed the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties label Oct 25, 2020
@ebroto ebroto added the S-waiting-on-author Status: This is awaiting some action from the author. (Use `@rustbot ready` to update this status) label Oct 25, 2020
@giraffate
Copy link
Contributor

ping from triage @cgm616. There seem to be some fixes left to be done. Do you have any questions on how to proceed here?

@bors
Copy link
Collaborator

bors commented Nov 25, 2020

☔ The latest upstream changes (presumably #6333) made this pull request unmergeable. Please resolve the merge conflicts.

Note that reviewers usually do not review pull requests until merge conflicts are resolved! Once you resolve the conflicts, you should change the labels applied by bors to indicate that your PR is ready for review. Post this as a comment to change the labels:

@rustbot modify labels: +S-waiting-on-review -S-waiting-on-author

@giraffate
Copy link
Contributor

@ebroto 2 weeks have passed with no activity from previous ping, so this should be closed accorging to Rust triage procedure.

@ebroto
Copy link
Member

ebroto commented Nov 26, 2020

Thanks for the heads-up, Takayuki ❤️

It breaks my heart having to close this as I think it's a cool lint. I will see if I can find some free time to address the last changes myself.

@ebroto ebroto closed this Nov 26, 2020
@ebroto ebroto added S-inactive-closed Status: Closed due to inactivity and removed S-waiting-on-author Status: This is awaiting some action from the author. (Use `@rustbot ready` to update this status) labels Nov 26, 2020
@cgm616
Copy link
Contributor Author

cgm616 commented Nov 26, 2020

I know this is closed for triage reasons, but I am going to try to finish this. My school semester is almost done and I've been too busy over the last few weeks, but I should be able to pull this over the finish line soon.

@blyxyas blyxyas removed the S-inactive-closed Status: Closed due to inactivity label Oct 9, 2023
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.

Catch xor vs power confusion
8 participants