-
Notifications
You must be signed in to change notification settings - Fork 1.6k
New lint: replace x.get(x.len()-1)
with x.last()
#3673
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
Comments
For a vector, |
Oh right, so it doesn't work for |
x[x.len()-1]
with x.last()
x.get(x.len()-1)
with x.last()
I think I could try to do this one (if nobody else is working on it). I'll try to be following the instructions in CONTRIBUTING.md, but I assume I can ask here if I need more help? |
Yes always. Or on irc, or on discord. |
Ok so I think I have something working, but I can't manage to get the UI tests to run. Every time I try, it says I've tried looking around and haven't found anything. Any idea what's going on? Here is the branch: https://github.com./HarrisonMc555/rust-clippy/tree/use_last Here is the UI test (link): #![warn(clippy::use_last)] // This gives an error
fn dont_use_last() -> Option<i32> {
let x = vec![2, 3, 5];
let last_element = x.get(x.len() - 1); // ~ERROR Use _.last()
last_element.map(|val| val + 1) // To avoid warnings
}
fn index_into_last() -> i32 {
let x = vec![2, 3, 5];
let last_element = x[x.len() - 1];
last_element + 1 // To avoid warnings
}
fn main() {
let expected_value: i32 = 5;
assert_eq!(dont_use_last(), Some(expected_value));
assert_eq!(index_into_last(), expected_value);
} |
You forgot to add reg.register_early_lint_pass(box foo_functions::FooFunctionsPass); to After registering the lint it should produce output. Currently in the making, but might be useful: #3824 |
Awesome, thanks for your help! I used the automatic update script which added it to the complexity lists, etc., but didn't do this. Whoops. I have one more question: how do I check to make sure that the vector that they're calling However, when I matched on Thanks again for the help! |
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`
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`
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`
The following can be replaced with
x.last()
:The text was updated successfully, but these errors were encountered: