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

indexing_slicing warns about using custom indexing operations #11525

Closed
KamilaBorowska opened this issue Sep 17, 2023 · 4 comments · Fixed by #12488
Closed

indexing_slicing warns about using custom indexing operations #11525

KamilaBorowska opened this issue Sep 17, 2023 · 4 comments · Fixed by #12488
Assignees
Labels
C-bug Category: Clippy is not doing the correct thing I-false-positive Issue: The lint was triggered on code it shouldn't have

Comments

@KamilaBorowska
Copy link
Contributor

KamilaBorowska commented Sep 17, 2023

Summary

indexing_slicing warns about use of custom indexing operators that cannot possibly fail. I think it may make sense to check whether an indexed type has get method, and not report a problem if it doesn't.

This was initially reported to me on https://codeberg.org/xfix/enum-map/issues/78.

Lint Name

indexing_slicing

Reproducer

I tried this code:

#![warn(clippy::indexing_slicing)]

use std::ops::Index;

struct BoolMap<T> {
    false_value: T,
    true_value: T,
}

impl<T> Index<bool> for BoolMap<T> {
    type Output = T;
    fn index(&self, index: bool) -> &T {
        if index { &self.true_value } else { &self.false_value }
    }
}

fn main() {
    let map = BoolMap { false_value: 2, true_value: 4 };
    println!("{}", map[true]);
}

I saw this happen:

warning: indexing may panic
  --> src/main.rs:19:20
   |
19 |     println!("{}", map[true]);
   |                    ^^^^^^^^^
   |
   = help: consider using `.get(n)` or `.get_mut(n)` instead
   = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#indexing_slicing
note: the lint level is defined here
  --> src/main.rs:1:9
   |
1  | #![warn(clippy::indexing_slicing)]
   |         ^^^^^^^^^^^^^^^^^^^^^^^^

I expected to see this happen:

No warning.

Version

rustc 1.72.0 (5680fa18f 2023-08-23)
binary: rustc
commit-hash: 5680fa18feaa87f3ff04063800aec256c3d4b4be
commit-date: 2023-08-23
host: x86_64-unknown-linux-gnu
release: 1.72.0
LLVM version: 16.0.5

Additional Labels

No response

@KamilaBorowska KamilaBorowska added C-bug Category: Clippy is not doing the correct thing I-false-positive Issue: The lint was triggered on code it shouldn't have labels Sep 17, 2023
@ronnodas
Copy link

Maybe it would be enough to have a configuration option to ignore certain types, such as enum_map::EnumMap and counter::Counter?

@8573
Copy link

8573 commented Mar 7, 2024

I encounter this with url::Url. Could Clippy at least check whether the type has a get method (url::Url does not) before suggesting to use it?

@Jacherr
Copy link
Contributor

Jacherr commented Mar 14, 2024

@rustbot claim

@ronnodas
Copy link

Only checking for a get method will still trigger the lint for counter::Counter (where Index when it exists does not panic and is equivalent to the get method). While that is still a false positive, it should probably be a separate issue.

@bors bors closed this as completed in 28e887f May 31, 2024
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-positive Issue: The lint was triggered on code it shouldn't have
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants