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

Possible bug in the identity_conversion lint. #4236

Closed
Sean1708 opened this issue Jun 26, 2019 · 2 comments
Closed

Possible bug in the identity_conversion lint. #4236

Sean1708 opened this issue Jun 26, 2019 · 2 comments

Comments

@Sean1708
Copy link

Sean1708 commented Jun 26, 2019

If it's not a bug then I think the error message should probably be improved, because I have absolutely no idea what it's trying to tell me.

I've got a (hopefully short enough) MWE here:

$ cargo clippy -V         
clippy 0.0.212 (265318d 2019-05-17)
$ tree
.
├── Cargo.toml
└── src
    └── main.rs

1 directory, 2 files
$ cat Cargo.toml 
[package]
name = "clippy_mwe"
version = "0.1.0"
authors = []
edition = "2018"

[dependencies]
$ cat src/main.rs 
fn main() {
        print_paths().unwrap();
}

fn maybe_get_iter(v: &[f64]) -> Result<impl Iterator<Item=&f64>, usize> {
        if v.len() > 1 {
                Ok(v.iter())
        } else {
                Err(v.len())
        }
}

fn print_paths() -> Result<(), u64> {
        let v = vec![1.0, 2.0, 3.0, 4.0];
        for path in maybe_get_iter(&v).map_err(|e| e as u64)? {
                println!("{:?}", path);
        }

        Ok(())
}
$ cargo run
   Compiling clippy_mwe v0.1.0 (/tmp/tmp.sA6y5tSSr9/clippy_mwe)
    Finished dev [unoptimized + debuginfo] target(s) in 0.36s
     Running `target/debug/clippy_mwe`
1.0
2.0
3.0
4.0
$ cargo clippy
    Checking clippy_mwe v0.1.0 (/tmp/tmp.sA6y5tSSr9/clippy_mwe)
warning: identical conversion
  --> src/main.rs:15:14
   |
15 |     for path in maybe_get_iter(&v).map_err(|e| e as u64)? {
   |                 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider removing `maybe_get_iter(&v).map_err(|e| e as u64)?()`: `maybe_get_iter(&v).map_err(|e| e as u64)?`
   |
   = note: #[warn(clippy::identity_conversion)] on by default
   = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#identity_conversion

    Finished dev [unoptimized + debuginfo] target(s) in 0.34s

From my experimentation so far it looks like you need a function that returns an iterator as the Ok variant of a Result, to use map_err on that result, and then to use the ? operator. If you don't have all three of those things then clippy seems to be fine with it.

@flip1995
Copy link
Member

Duplicate of #4207 #4183 #4133 #3913 ... 😄

This is already fixed in Clippy master / nightly and probably beta. In stable Clippy it will be available at 4th of July.

If you have the latest Clippy and you still get this FP, feel free to reopen!

@Sean1708
Copy link
Author

Ah thanks. I checked the open tickets but forgot to check closed ones, sorry!

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

No branches or pull requests

2 participants