Skip to content

False positive for redundant field names when using range? #2799

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

Closed
avranju opened this issue May 25, 2018 · 10 comments
Closed

False positive for redundant field names when using range? #2799

avranju opened this issue May 25, 2018 · 10 comments
Labels
C-bug Category: Clippy is not doing the correct thing

Comments

@avranju
Copy link

avranju commented May 25, 2018

For the following code:

fn main() {
    let start = 0;
    let fin = 10;
    let vals: Vec<i32> = (start..fin).map(|v| v * v).collect();
    println!("{:?}", vals);
}

Clippy warns:

warning: redundant field names in struct initialization
 --> src/main.rs:4:27
  |
4 |     let vals: Vec<i32> = (start..fin).map(|v| v * v).collect();
  |                           ^^^^^ help: replace it with: `start`
  |
  = note: #[warn(redundant_field_names)] on by default
  = help: for further information visit https://rust-lang-nursery.github.io/rust-clippy/v0.0.204/index.html#redundant_field_names

Is this a legitimate lint?

@brinchj
Copy link

brinchj commented May 25, 2018

Hi!

As far as I can tell, it looks like #2507 tried to address the issue.

But I can still reproduce it in the Rust playground (tools -> clippy):
https://play.rust-lang.org/?gist=8e1d9d87643e9799fce1c60303ea3bab

I wonder which version includes the PR...

@brinchj
Copy link

brinchj commented May 25, 2018

I can reproduce this locally with Clippy 0.0.198.

@ordovicia
Copy link
Contributor

ordovicia commented May 25, 2018

Latest clippy (v0.0.204) does not lint start..end,
but does (start..end).collect::<Vec<i32>>() and (start..end).map(|v| v * v).collect::<Vec<i32>>().

https://play.rust-lang.org/?gist=3cbade6f25fd49565712e71d0a32de78&version=stable&mode=debug

@flip1995
Copy link
Member

It seems that (start..end) has no expansion info, while start..end does.

https://github.com/rust-lang-nursery/rust-clippy/blob/400aab92325de2aa65a2c724c3b5f4337bca11c0/clippy_lints/src/utils/mod.rs#L68-L75

A possible (easy) solution would be to just ignore Range-structs, but I'm not sure if this would be the right thing to do... (already discussed here: #2507 (review)).

@phansch phansch added the C-bug Category: Clippy is not doing the correct thing label May 31, 2018
@ghost
Copy link

ghost commented Jun 2, 2018

I think this is a compiler bug.

When lowering expressions in the compiler, it looks like they go to a lot of trouble to set up a span with the correct context and then they replace it if it's enclosed in a parenthesis. See this branch and this branch in lower_expr. 🤔

@ghost
Copy link

ghost commented Aug 8, 2018

Fixed by #3007.

@flip1995
Copy link
Member

flip1995 commented Aug 8, 2018

There should be a test for this, before closing this issue.

@flip1995
Copy link
Member

flip1995 commented Aug 8, 2018

These tests are for the unparenthesized case. The bug in this issue is that it didn't work with (start..end)

@ghost
Copy link

ghost commented Aug 8, 2018

OK, I'll submit the PR after the build issues are sorted out.

flip1995 added a commit that referenced this issue Aug 14, 2018
Add a test to ensure that #2799 is fixed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
C-bug Category: Clippy is not doing the correct thing
Projects
None yet
Development

No branches or pull requests

5 participants