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

Implement "Use last" lint #3832

Merged
merged 2 commits into from
May 27, 2019
Merged

Implement "Use last" lint #3832

merged 2 commits into from
May 27, 2019

Conversation

HarrisonMc555
Copy link
Contributor

@HarrisonMc555 HarrisonMc555 commented Feb 28, 2019

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

@phansch phansch added the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties label Mar 1, 2019
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.

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"

@@ -6,7 +6,8 @@
#![feature(stmt_expr_attributes)]
#![feature(range_contains)]
#![allow(clippy::missing_docs_in_private_items)]
#![recursion_limit = "256"]
// #![recursion_limit = "256"]
Copy link
Contributor

Choose a reason for hiding this comment

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

remove this comment

// 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;
Copy link
Contributor

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.

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

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

@flip1995 flip1995 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 Mar 9, 2019
@phansch
Copy link
Member

phansch commented Apr 8, 2019

friendly ping @HarrisonMc555 would you like to continue with this? Feel free to ask questions if you're stuck somewhere.

@bors
Copy link
Collaborator

bors commented Apr 8, 2019

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

@HarrisonMc555
Copy link
Contributor Author

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!

@HarrisonMc555
Copy link
Contributor Author

HarrisonMc555 commented Apr 16, 2019

I believe I finished everything, sorry it took a while. I can squash if you want.

Also, I couldn't run rustfmt. When I run cargo fmt I get:

error: 'cargo-fmt' is not installed for the toolchain 'master'

When I run rustup component add rustfmt, I get:

error: toolchain 'master' does not support components

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?

@oli-obk
Copy link
Contributor

oli-obk commented Apr 17, 2019

You can use cargo +nightly fmt

@bors
Copy link
Collaborator

bors commented Apr 18, 2019

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

@HarrisonMc555
Copy link
Contributor Author

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 rustfmt (actually I ran rustfmt afterwards but it didn't change anything, thank goodness).

I think we should be good, we'll see in a few minutes if the CI build passes now. Thanks for the help everyone!

@HarrisonMc555
Copy link
Contributor Author

So Travis CI failed, and I'm not sure why. I just re-ran git pull upstream master into my master branch and git merge master into my use_last branch.

I then ran cargo test, and all the tests pass. Any tips on resolving this? I'm not sure what's going on.

@andrehjr
Copy link
Contributor

@HarrisonMc555 Seems like it's still failing because of cargo fmt 🤔 maybe you need to update rustfmt? Anyway, if you fix the following, you should be good to go.

Diff in /Users/travis/build/rust-lang/rust-clippy/clippy_lints/src/use_last.rs at line 1:
 //! lint on using `x.get(x.len() - 1)` instead of `x.last()`
 
-use crate::utils::{match_type, paths, span_lint_and_sugg,
-                   snippet_with_applicability, SpanlessEq};
+use crate::utils::{match_type, paths, snippet_with_applicability, span_lint_and_sugg, SpanlessEq};
+use if_chain::if_chain;
+use rustc::hir::{Expr, ExprKind};
 use rustc::lint::{LateContext, LateLintPass, LintArray, LintPass};
 use rustc::{declare_tool_lint, lint_array};
-use rustc::hir::{Expr, ExprKind};
 use rustc_errors::Applicability;
-use syntax::ast::{LitKind};
-use if_chain::if_chain;
+use syntax::ast::LitKind;
 
 /// **What it does:** Checks for using `x.get(x.len() - 1)` instead of `x.last()`.
 ///
The command "if [ -z ${INTEGRATION} ]; then```

@HarrisonMc555
Copy link
Contributor Author

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 declare_lint_pass! macro. Hopefully this passes CI now :-)

@HarrisonMc555
Copy link
Contributor Author

@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!

@HarrisonMc555
Copy link
Contributor Author

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.

tests/ui/use_last.rs Outdated Show resolved Hide resolved
@phansch
Copy link
Member

phansch commented Apr 21, 2019

Now, the Linux CI build passes but the macOS build fails.

Woops, I forgot to submit the comment yesterday. It was only a spurious network issue and a restart of the Travis build fixed it.

clippy_lints/src/use_last.rs Outdated Show resolved Hide resolved
clippy_lints/src/use_last.rs Outdated Show resolved Hide resolved
clippy_lints/src/use_last.rs Outdated Show resolved Hide resolved
clippy_lints/src/use_last.rs Outdated Show resolved Hide resolved
clippy_lints/src/use_last.rs Outdated Show resolved Hide resolved
clippy_lints/src/use_last.rs Outdated Show resolved Hide resolved
@bors
Copy link
Collaborator

bors commented Apr 30, 2019

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

@HarrisonMc555
Copy link
Contributor Author

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.

@flip1995
Copy link
Member

You could push to a branch unrelated to this PR and when you're finished merge that branch into the branch of this PR.

@HarrisonMc555
Copy link
Contributor Author

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.

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.

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

clippy_lints/src/get_last_with_len.rs Outdated Show resolved Hide resolved
clippy_lints/src/get_last_with_len.rs Outdated Show resolved Hide resolved
clippy_lints/src/get_last_with_len.rs Outdated Show resolved Hide resolved
tests/ui/get_last_with_len.stderr Outdated Show resolved Hide resolved
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.

This LGTM now. Thanks for all the work!

Just 2 formatting things and squashing the commits and this is good to go.

clippy_lints/src/get_last_with_len.rs Outdated Show resolved Hide resolved
clippy_lints/src/get_last_with_len.rs Outdated Show resolved Hide resolved
@bors
Copy link
Collaborator

bors commented May 20, 2019

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

@Manishearth
Copy link
Member

Can you rebase over master instead of merging?

@Manishearth
Copy link
Member

It may be easier to create a fresh commit with the diff from the PR

@HarrisonMc555
Copy link
Contributor Author

@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?

@HarrisonMc555
Copy link
Contributor Author

Did I do this right? I started doing git rebase -i but there were like 37 steps and they seemed redundant. So I just created a patch from master, reset back to master, applied the patch, and did git push --force-with-lease. Let me know if that's the right idea or if I need to do the tedious git rebase -i stuff 😄

@Manishearth
Copy link
Member

That's fine! That's what I meant by creating a fresh commit.

@HarrisonMc555
Copy link
Contributor Author

Ok great! So is everything done now? I think GitHub is saying there's still a change requested from you @Manishearth.

@flip1995
Copy link
Member

flip1995 commented May 21, 2019

Yes, that's all. Thanks for all the work!

@bors r+

@bors
Copy link
Collaborator

bors commented May 21, 2019

📌 Commit 3271491 has been approved by flip1995

bors added a commit that referenced this pull request May 21, 2019
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`
@bors
Copy link
Collaborator

bors commented May 21, 2019

⌛ Testing commit 3271491 with merge 4a2ff35...

@bors
Copy link
Collaborator

bors commented May 21, 2019

💡 This pull request was already approved, no need to approve it again.

  • This pull request is currently being tested. If there's no response from the continuous integration service, you may use retry to trigger a build again.

@bors
Copy link
Collaborator

bors commented May 21, 2019

📌 Commit 3271491 has been approved by flip1995

@bors
Copy link
Collaborator

bors commented May 21, 2019

⌛ Testing commit 3271491 with merge 64f4f62...

bors added a commit that referenced this pull request May 21, 2019
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`
@bors
Copy link
Collaborator

bors commented May 21, 2019

💔 Test failed - checks-travis

@flip1995
Copy link
Member

@flip1995 flip1995 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 May 22, 2019
@HarrisonMc555
Copy link
Contributor Author

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?

@flip1995
Copy link
Member

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)

@bors
Copy link
Collaborator

bors commented May 27, 2019

📌 Commit 42d849c has been approved by flip1995

@bors
Copy link
Collaborator

bors commented May 27, 2019

⌛ Testing commit 42d849c with merge cf81a3b...

bors added a commit that referenced this pull request May 27, 2019
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`
@bors
Copy link
Collaborator

bors commented May 27, 2019

☀️ Test successful - checks-travis, status-appveyor
Approved by: flip1995
Pushing cf81a3b to master...

@bors bors merged commit 42d849c into rust-lang:master May 27, 2019
@HarrisonMc555 HarrisonMc555 deleted the use_last branch May 28, 2019 03:48
@HarrisonMc555
Copy link
Contributor Author

Thanks @flip1995! No problem :-)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
S-waiting-on-review Status: Awaiting review from the assignee but also interested parties
Projects
None yet
Development

Successfully merging this pull request may close these issues.

New lint: replace x.get(x.len()-1) with x.last()
7 participants