Avoid unnecessary String allocations in MinifyingSugg arithmetic ops#17057
Conversation
|
r? @llogiq rustbot has assigned @llogiq. Use Why was this reviewer chosen?The reviewer was selected based on:
|
| self.0 | ||
| } | ||
|
|
||
| #[inline] |
There was a problem hiding this comment.
Nit: this should not be needed, as a mod private function it will be inlined if the compiler thinks this is a good idea.
|
Reminder, once the PR becomes ready for a review, use |
|
@rustbot ready |
|
Please squash the commits together (and "u" is hardly an acceptable commit message anyway, typo?). |
11c156d to
7c1c1b0
Compare
Sorry about that. I usually use a simple "u" as shorthand for "update", since in previous contributions these commits were always squashed into a single commit named after the PR title, without keeping the individual commit messages. |
Summary
In
clippy_lints/src/loops/manual_memcpy.rs, the fourAdd/Subtrait impls onMinifyingSuggused the pattern:Every call therefore allocated two
Strings purely so they could be matched against the literal"0". The allocations were discarded immediately afterwards.This PR:
is_zero()helper that pattern-matchesSugg::NonParen("0")without allocating. In this lint's data flowSugg::NonParenis the only variant that can carry the textual value"0"(seesugg::ZEROand the literal path inget_offset), so the check is equivalent to the originalto_string() == "0".Add/Subimpls to useis_zero()for the zero-side fast paths.self.to_string() == rhs.to_string()for thea - a = 0collapse insideSub, 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