Skip to content

indexing_slicing is not triggered in const fns #9808

Closed
@KizzyCode

Description

@KizzyCode

Summary

The indexing_slicing-lint is not triggered within const fns. In the following example, is_negative performs indexing on a slice (self.value[0]) which should get linted but isn't. However if I remove the const-attribute for is_negative, the lint gets triggered as expected.

#![warn(clippy::indexing_slicing)]

/// An opaque integer representation
pub struct Integer<'a> {
    /// The underlying data
    value: &'a [u8],
}
impl<'a> Integer<'a> {
    // Check whether `self` holds a negative number or not
    pub const fn is_negative(&self) -> bool {
        self.value[0] & 0b1000_0000 != 0
    }
}

Lint Name

indexing_slicing

Reproducer

I tried this code:

#![warn(clippy::indexing_slicing)]

/// An opaque integer representation
pub struct Integer<'a> {
    /// The underlying data
    value: &'a [u8],
}
impl<'a> Integer<'a> {
    // Check whether `self` holds a negative number or not
    pub const fn is_negative(&self) -> bool {
        self.value[0] & 0b1000_0000 != 0
    }
}

I expected to see this happen: The lint warns me on self.value[0].

Instead, this happened: The lint does not warn me on self.value[0] 😅

Version

rustc 1.65.0 (897e37553 2022-11-02)
binary: rustc
commit-hash: 897e37553bba8b42751c67658967889d11ecd120
commit-date: 2022-11-02
host: aarch64-apple-darwin
release: 1.65.0
LLVM version: 15.0.0

Metadata

Metadata

Assignees

Labels

C-bugCategory: Clippy is not doing the correct thingI-false-negativeIssue: The lint should have been triggered on code, but wasn'tgood first issueThese issues are a good way to get started with Clippy

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions