-
Notifications
You must be signed in to change notification settings - Fork 1.8k
Open
Labels
C-bugCategory: Clippy is not doing the correct thingCategory: Clippy is not doing the correct thingI-false-positiveIssue: The lint was triggered on code it shouldn't haveIssue: The lint was triggered on code it shouldn't have
Description
Summary
When calling str::ends_with
with a &str
, Rust doesn't know that, for the method to return true
, the length of the haystack has to be greater than or equal to the length of the needle.
Lint Name
arithmetic_side_effects
Reproducer
I tried this code:
fn main() {
let mut a = "abc".to_string();
let b = "c";
if a.ends_with(b) {a.truncate(a.len()-b.len())};
}
with --warn clippy::arithmetic_side_effects
I saw this happen:
warning: arithmetic operation that can potentially result in unexpected side-effects
--> src/main.rs:4:35
|
4 | if a.ends_with(b) {a.truncate(a.len()-b.len())};
| ^^^^^^^^^^^^^^^
|
= help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#arithmetic_side_effects
= note: requested on the command line with `-W clippy::arithmetic-side-effects`
warning: `asetest` (bin "asetest") generated 1 warning
Finished dev [unoptimized + debuginfo] target(s) in 0.05s
I expected to see this happen:
No warning because the thing being warned about can't happen.
Version
rustc 1.77.2 (25ef9e3d8 2024-04-09)
binary: rustc
commit-hash: 25ef9e3d85d934b27d9dada2f9dd52b1dc63bb04
commit-date: 2024-04-09
host: x86_64-unknown-linux-gnu
release: 1.77.2
LLVM version: 17.0.6
Additional Labels
No response
Metadata
Metadata
Assignees
Labels
C-bugCategory: Clippy is not doing the correct thingCategory: Clippy is not doing the correct thingI-false-positiveIssue: The lint was triggered on code it shouldn't haveIssue: The lint was triggered on code it shouldn't have