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

Imporve redundant_slicing lint by taking the expected type into account #7257

Closed
YohDeadfall opened this issue May 20, 2021 · 3 comments · Fixed by #8218
Closed

Imporve redundant_slicing lint by taking the expected type into account #7257

YohDeadfall opened this issue May 20, 2021 · 3 comments · Fixed by #8218
Labels
C-bug Category: Clippy is not doing the correct thing I-false-negative Issue: The lint should have been triggered on code, but wasn't

Comments

@YohDeadfall
Copy link
Contributor

YohDeadfall commented May 20, 2021

Lint name: redundant_slicing

I tried this code:

fn main() {
    let v = vec![1, 2, 3];
    test_vec(&v[..]);

    let v = [1, 2, 3];
    test_vec(&v[..]);

    let s = "Hello, World!";
    test_str(&s[..]);

    let s = "Hello, World!".to_string();
    test_str(&s[..]);
}

fn test_vec(_: &[i32]) {
}

fn test_str(_: &str) {
}

I expected to see a warning for each call, but instead only 9th is reported since types before and after indexing are same. It would be much better if clippy is able to see what type is expected by a higher expression/parameter.

If it's possible, I would like to do it myself.

Meta

  • cargo clippy -V: clippy 0.1.53 (bacf770 2021-05-05)
  • rustc -Vv:
    rustc 1.54.0-nightly (bacf770f2 2021-05-05)
    binary: rustc
    commit-hash: bacf770f2983a52f31e3537db5f0fe1ef2eaa874
    commit-date: 2021-05-05
    host: x86_64-pc-windows-msvc
    release: 1.54.0-nightly
    LLVM version: 12.0.0
    
@YohDeadfall YohDeadfall added C-bug Category: Clippy is not doing the correct thing I-false-negative Issue: The lint should have been triggered on code, but wasn't labels May 20, 2021
@Jarcho
Copy link
Contributor

Jarcho commented May 21, 2021

You're essentially checking if the result of dereferencing the value would have the same type as slicing. You'll also need to check to make sure auto-deref will happen when the change is made (this is the hard part).

@YohDeadfall
Copy link
Contributor Author

I found the algorithm used by auto deref, but what I can't understand is how to get traits implemented by a struct from a LateContext.

@giraffate
Copy link
Contributor

I don't know the details of the implementation, but FYI this might be useful: https://github.com/rust-lang/rust-clippy/blob/master/doc/common_tools_writing_lints.md#checking-if-a-type-implements-a-specific-trait

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 I-false-negative Issue: The lint should have been triggered on code, but wasn't
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants