Commit 7019104
authored
## Summary
In `clippy_lints/src/loops/manual_memcpy.rs`, the four `Add`/`Sub` trait
impls on `MinifyingSugg` used the pattern:
```rust
match (self.to_string().as_str(), rhs.to_string().as_str()) {
("0", _) => rhs.clone(),
(_, "0") => self.clone(),
...
}
```
Every call therefore allocated two `String`s purely so they could be
matched against the literal `"0"`. The allocations were discarded
immediately afterwards.
This PR:
- Adds an inline `is_zero()` helper that pattern-matches
`Sugg::NonParen("0")` without allocating. In this lint's data flow
`Sugg::NonParen` is the only variant that can carry the textual value
`"0"` (see `sugg::ZERO` and the literal path in `get_offset`), so the
check is equivalent to the original `to_string() == "0"`.
- Rewrites the four `Add`/`Sub` impls to use `is_zero()` for the
zero-side fast paths.
- Keeps `self.to_string() == rhs.to_string()` for the `a - a = 0`
collapse inside `Sub`, but only as the final branch — it now only runs
when both operands are confirmed non-zero, so the common case is fully
allocation-free.
changelog: none
1 file changed
Lines changed: 26 additions & 16 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
230 | 230 | | |
231 | 231 | | |
232 | 232 | | |
| 233 | + | |
| 234 | + | |
| 235 | + | |
| 236 | + | |
233 | 237 | | |
234 | 238 | | |
235 | 239 | | |
| |||
241 | 245 | | |
242 | 246 | | |
243 | 247 | | |
244 | | - | |
245 | | - | |
246 | | - | |
| 248 | + | |
| 249 | + | |
| 250 | + | |
247 | 251 | | |
248 | 252 | | |
249 | 253 | | |
| |||
252 | 256 | | |
253 | 257 | | |
254 | 258 | | |
255 | | - | |
256 | | - | |
257 | | - | |
258 | | - | |
259 | | - | |
| 259 | + | |
| 260 | + | |
| 261 | + | |
| 262 | + | |
| 263 | + | |
| 264 | + | |
| 265 | + | |
| 266 | + | |
260 | 267 | | |
261 | 268 | | |
262 | 269 | | |
263 | 270 | | |
264 | 271 | | |
265 | 272 | | |
266 | 273 | | |
267 | | - | |
268 | | - | |
269 | | - | |
| 274 | + | |
| 275 | + | |
| 276 | + | |
270 | 277 | | |
271 | 278 | | |
272 | 279 | | |
| |||
275 | 282 | | |
276 | 283 | | |
277 | 284 | | |
278 | | - | |
279 | | - | |
280 | | - | |
281 | | - | |
282 | | - | |
| 285 | + | |
| 286 | + | |
| 287 | + | |
| 288 | + | |
| 289 | + | |
| 290 | + | |
| 291 | + | |
| 292 | + | |
283 | 293 | | |
284 | 294 | | |
285 | 295 | | |
| |||
0 commit comments