-
Notifications
You must be signed in to change notification settings - Fork 1.5k
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
Implement "Use last" lint #3832
Conversation
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.
You can squash commits by using
git rebase -i origin/master
(assuming the clippy repo is "origin" in your setup). That should open an editor which includes instructions. Mostly it's changing all but the first commit to "fixup" instead of "pick"
clippy_lints/src/lib.rs
Outdated
@@ -6,7 +6,8 @@ | |||
#![feature(stmt_expr_attributes)] | |||
#![feature(range_contains)] | |||
#![allow(clippy::missing_docs_in_private_items)] | |||
#![recursion_limit = "256"] | |||
// #![recursion_limit = "256"] |
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.
remove this comment
clippy_lints/src/use_last.rs
Outdated
// let _ = println!("LHS of sub method has an arg"); | ||
|
||
// TODO: Is this a valid way to check if they reference the same vector? | ||
// if arg_lhs_struct.hir_id == struct_calling_on.hir_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.
you need to check whether the two expressions are equal by structure. I think we have an expr_eq
function or similar code somewhere in clippy.
tests/ui/use_last.rs
Outdated
fn dont_use_last() -> Option<i32> { | ||
let x = vec![2, 3, 5]; | ||
let last_element = x.get(x.len() - 1); // ~ERROR Use x.last() | ||
last_element.map(|val| val + 1) // To avoid warnings |
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.
you could just use let _ = x.get ....
. The underscore silences the warnings, too
friendly ping @HarrisonMc555 would you like to continue with this? Feel free to ask questions if you're stuck somewhere. |
☔ The latest upstream changes (presumably #3848) made this pull request unmergeable. Please resolve the merge conflicts. |
Yes, sorry. I've been caught up with other things. I'll try to get this done within the next week or two. Thanks for the ping! |
I believe I finished everything, sorry it took a while. I can squash if you want. Also, I couldn't run
When I run
I looked for an issue and I saw a merged pull request about switching to nightly (#3586) but I couldn't get that to work either. Can we add something to adding_lints.md? |
You can use |
☔ The latest upstream changes (presumably #3968) made this pull request unmergeable. Please resolve the merge conflicts. |
I was wondering why the CI build failed...I forgot how fast things can change :-) I just merged the changes, updated the README, and ran I think we should be good, we'll see in a few minutes if the CI build passes now. Thanks for the help everyone! |
So Travis CI failed, and I'm not sure why. I just re-ran I then ran |
@HarrisonMc555 Seems like it's still failing because of
|
Ok I finally figured out how to run rustfmt. This is how I did it: rustup component add --toolchain=nightly rustfmt
rustup run nightly rustfmt path/to/file.rs Also I used the new |
@andrehjr everything passed now, looks like we should be able to merge. Let me know if there's anything else that needs to be done! |
Hmm so I had a passing CI build, but some other commits were pushed before this was merged. I pulled the new commits and everything merged on my machine fine. Now, the Linux CI build passes but the macOS build fails. How do I go about fixing this? I don't have a macOS machine available. |
Woops, I forgot to submit the comment yesterday. It was only a spurious network issue and a restart of the Travis build fixed it. |
☔ The latest upstream changes (presumably #4039) made this pull request unmergeable. Please resolve the merge conflicts. |
I have some more time this week, so I should get it done within the next day or two. However, I end up working on a computer both at school and at home, so I'm pushing and pulling to keep them in sync...which I believe will trigger Travis builds, even when I don't intend it to. Is there any way to stop this? I just feel guilty causing an unnecessary build takes time away from necessary builds. |
You could push to a branch unrelated to this PR and when you're finished merge that branch into the branch of this PR. |
Ok, I think the pull request is ready. All the tests pass, and I believe I've addressed all the concerns. I've left the "conversations" open that I didn't take as literal suggestions. Sorry for not using the new GitHub built-in suggestion inserter thing. |
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.
rustfmt doesn't format code inside macros. Please format the code by hand, so it more or less matches the style of the codebase.
Please also remove the tests/ui/get_last_with_len.stdout
file
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 LGTM now. Thanks for all the work!
Just 2 formatting things and squashing the commits and this is good to go.
☔ The latest upstream changes (presumably #4088) made this pull request unmergeable. Please resolve the merge conflicts. |
Can you rebase over master instead of merging? |
It may be easier to create a fresh commit with the diff from the PR |
@Manishearth sure thing. What is the recommended way to do that? Do I create a patch with all the changes and add it to a new branch or delete the old commits from this branch or what? |
Did I do this right? I started doing |
That's fine! That's what I meant by creating a fresh commit. |
Ok great! So is everything done now? I think GitHub is saying there's still a change requested from you @Manishearth. |
Yes, that's all. Thanks for all the work! @bors r+ |
📌 Commit 3271491 has been approved by |
Implement "Use last" lint Closes #3673 This lint checks the use of `x.get(x.len() - 1)` and suggests `x.last()` (where `x` is a `Vec`). There's at least one more thing that needs to happen here. I believe I am correctly checking most of the scenarios and avoiding false positives. However, if different `Vec`s are used for the `x.get` and `y.len`, then it will emit a false positive. In other words, I need to check to make sure the `Vec`s are the same. Also, a lot of these commits were temporary and not helpful to the project history...am I supposed to squash on merge? If so, how do I do that? changelog: New lint: `get_last_with_len`
💡 This pull request was already approved, no need to approve it again.
|
📌 Commit 3271491 has been approved by |
Implement "Use last" lint Closes #3673 This lint checks the use of `x.get(x.len() - 1)` and suggests `x.last()` (where `x` is a `Vec`). There's at least one more thing that needs to happen here. I believe I am correctly checking most of the scenarios and avoiding false positives. However, if different `Vec`s are used for the `x.get` and `y.len`, then it will emit a false positive. In other words, I need to check to make sure the `Vec`s are the same. Also, a lot of these commits were temporary and not helpful to the project history...am I supposed to squash on merge? If so, how do I do that? changelog: New lint: `get_last_with_len`
💔 Test failed - checks-travis |
There was a minor hiccup when attempting to merge formatting suggestions but it's been resolved. All tests are passing and all changes are approved. Are we ready to merge? There have been a few commits since I squashed, do you want me to merge, re-squash, and update the branch again? |
No it should be good as is, so this should work: @bors r+ (Sorry, I was busy the past 4 days) |
📌 Commit 42d849c has been approved by |
Implement "Use last" lint Closes #3673 This lint checks the use of `x.get(x.len() - 1)` and suggests `x.last()` (where `x` is a `Vec`). There's at least one more thing that needs to happen here. I believe I am correctly checking most of the scenarios and avoiding false positives. However, if different `Vec`s are used for the `x.get` and `y.len`, then it will emit a false positive. In other words, I need to check to make sure the `Vec`s are the same. Also, a lot of these commits were temporary and not helpful to the project history...am I supposed to squash on merge? If so, how do I do that? changelog: New lint: `get_last_with_len`
☀️ Test successful - checks-travis, status-appveyor |
Thanks @flip1995! No problem :-) |
Closes #3673
This lint checks the use of
x.get(x.len() - 1)
and suggestsx.last()
(wherex
is aVec
).There's at least one more thing that needs to happen here. I believe I am correctly checking most of the scenarios and avoiding false positives. However, if different
Vec
s are used for thex.get
andy.len
, then it will emit a false positive. In other words, I need to check to make sure theVec
s are the same.Also, a lot of these commits were temporary and not helpful to the project history...am I supposed to squash on merge? If so, how do I do that?
changelog: New lint:
get_last_with_len