-
Notifications
You must be signed in to change notification settings - Fork 2k
Add new lint manual_isolate_lowest_one #17010
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
Open
b9nn
wants to merge
2
commits into
rust-lang:master
Choose a base branch
from
b9nn:lint/16967-manual-isolate-lowest-one
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,113 @@ | ||
| use clippy_config::Conf; | ||
| use clippy_utils::diagnostics::span_lint_and_sugg; | ||
| use clippy_utils::msrvs::{self, Msrv}; | ||
| use clippy_utils::sugg::Sugg; | ||
| use clippy_utils::{SpanlessEq, is_from_proc_macro, sym}; | ||
| use rustc_errors::Applicability; | ||
| use rustc_hir::{BinOpKind, Expr, ExprKind, UnOp}; | ||
| use rustc_lint::{LateContext, LateLintPass}; | ||
| use rustc_session::impl_lint_pass; | ||
| use rustc_span::SyntaxContext; | ||
|
|
||
| declare_clippy_lint! { | ||
| /// ### What it does | ||
| /// Checks for expressions like `x & -x` or `x & x.wrapping_neg()`, which are manual | ||
| /// reimplementations of `x.isolate_lowest_one()`. | ||
| /// | ||
| /// ### Why is this bad? | ||
| /// `x.isolate_lowest_one()` is clearer than the bitwise trick. It also avoids the | ||
| /// overflow that occurs when `x == T::MIN` for signed types using the `-` operator, | ||
| /// and preserves non-zero type information for `NonZero<T>`. | ||
| /// | ||
| /// ### Example | ||
| /// ```no_run | ||
| /// let x: u32 = 5; | ||
| /// let lsb = x & x.wrapping_neg(); | ||
| /// ``` | ||
| /// Use instead: | ||
| /// ```no_run | ||
| /// let x: u32 = 5; | ||
| /// let lsb = x.isolate_lowest_one(); | ||
| /// ``` | ||
| #[clippy::version = "1.97.0"] | ||
| pub MANUAL_ISOLATE_LOWEST_ONE, | ||
| complexity, | ||
| "manually reimplementing `isolate_lowest_one`" | ||
| } | ||
|
|
||
| impl_lint_pass!(ManualIsolateLowestOne => [MANUAL_ISOLATE_LOWEST_ONE]); | ||
|
|
||
| pub struct ManualIsolateLowestOne { | ||
| msrv: Msrv, | ||
| } | ||
|
|
||
| impl ManualIsolateLowestOne { | ||
| pub fn new(conf: &'static Conf) -> Self { | ||
| Self { msrv: conf.msrv } | ||
| } | ||
| } | ||
|
|
||
| impl<'tcx> LateLintPass<'tcx> for ManualIsolateLowestOne { | ||
| fn check_expr(&mut self, cx: &LateContext<'tcx>, expr: &Expr<'tcx>) { | ||
| if expr.span.from_expansion() { | ||
| return; | ||
| } | ||
|
|
||
| let ExprKind::Binary(op, lhs, rhs) = expr.kind else { | ||
| return; | ||
| }; | ||
| if op.node != BinOpKind::BitAnd || lhs.span.from_expansion() || rhs.span.from_expansion() { | ||
| return; | ||
| } | ||
|
|
||
| let ctxt = expr.span.ctxt(); | ||
| let recv = is_negation_pair(cx, ctxt, lhs, rhs).or_else(|| is_negation_pair(cx, ctxt, rhs, lhs)); | ||
| let Some(recv) = recv else { return }; | ||
|
|
||
| if !cx.typeck_results().expr_ty_adjusted(recv).is_integral() { | ||
| return; | ||
| } | ||
|
|
||
| if !self.msrv.meets(cx, msrvs::ISOLATE_LOWEST_ONE) { | ||
| return; | ||
| } | ||
|
|
||
| if is_from_proc_macro(cx, expr) { | ||
| return; | ||
| } | ||
|
|
||
| let mut applicability = Applicability::MachineApplicable; | ||
| let snippet = Sugg::hir_with_context(cx, recv, ctxt, "_", &mut applicability); | ||
|
|
||
| span_lint_and_sugg( | ||
| cx, | ||
| MANUAL_ISOLATE_LOWEST_ONE, | ||
| expr.span, | ||
| "manually reimplementing `isolate_lowest_one`", | ||
| "consider using `.isolate_lowest_one()`", | ||
| format!("{}.isolate_lowest_one()", snippet.maybe_paren()), | ||
| applicability, | ||
| ); | ||
| } | ||
| } | ||
|
|
||
| /// Returns `Some(base)` if `negated` is `-base` or `base.wrapping_neg()` (where `base` is | ||
| /// structurally equal to `expected_base`). | ||
| fn is_negation_pair<'tcx>( | ||
| cx: &LateContext<'tcx>, | ||
| ctxt: SyntaxContext, | ||
| expected_base: &'tcx Expr<'tcx>, | ||
| negated: &'tcx Expr<'tcx>, | ||
| ) -> Option<&'tcx Expr<'tcx>> { | ||
| match negated.kind { | ||
| ExprKind::Unary(UnOp::Neg, inner) if SpanlessEq::new(cx).eq_expr(ctxt, expected_base, inner) => { | ||
| Some(expected_base) | ||
| }, | ||
| ExprKind::MethodCall(method, inner, [], _) | ||
| if method.ident.name == sym::wrapping_neg && SpanlessEq::new(cx).eq_expr(ctxt, expected_base, inner) => | ||
| { | ||
| Some(expected_base) | ||
| }, | ||
| _ => None, | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,34 @@ | ||
| //@aux-build:proc_macros.rs | ||
| #![warn(clippy::manual_isolate_lowest_one)] | ||
| #![allow(clippy::unnecessary_operation, clippy::no_effect, dead_code)] | ||
|
|
||
| use proc_macros::{external, with_span}; | ||
|
|
||
| fn unsigned(a: u32, b: u64) { | ||
| let _ = a.isolate_lowest_one(); //~ manual_isolate_lowest_one | ||
| let _ = a.isolate_lowest_one(); //~ manual_isolate_lowest_one | ||
| let _ = b.isolate_lowest_one(); //~ manual_isolate_lowest_one | ||
| } | ||
|
|
||
| fn signed(a: i32) { | ||
| let _ = a.isolate_lowest_one(); //~ manual_isolate_lowest_one | ||
| let _ = a.isolate_lowest_one(); //~ manual_isolate_lowest_one | ||
| let _ = a.isolate_lowest_one(); //~ manual_isolate_lowest_one | ||
| } | ||
|
|
||
| fn no_lint_different_operand(a: i32, c: i32) { | ||
| // Different operands — must not lint. | ||
| let _ = a & c.wrapping_neg(); | ||
| let _ = a & -c; | ||
| } | ||
|
|
||
| fn no_lint_macros(a: u32) { | ||
| macro_rules! same { | ||
| ($x:expr) => { | ||
| $x & $x.wrapping_neg() | ||
| }; | ||
| } | ||
| same!(a); | ||
| external!($a & $a.wrapping_neg()); | ||
| with_span!(span; a & a.wrapping_neg()); | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,34 @@ | ||
| //@aux-build:proc_macros.rs | ||
| #![warn(clippy::manual_isolate_lowest_one)] | ||
| #![allow(clippy::unnecessary_operation, clippy::no_effect, dead_code)] | ||
|
|
||
| use proc_macros::{external, with_span}; | ||
|
|
||
| fn unsigned(a: u32, b: u64) { | ||
| let _ = a & a.wrapping_neg(); //~ manual_isolate_lowest_one | ||
| let _ = a.wrapping_neg() & a; //~ manual_isolate_lowest_one | ||
| let _ = b & b.wrapping_neg(); //~ manual_isolate_lowest_one | ||
| } | ||
|
|
||
| fn signed(a: i32) { | ||
| let _ = a & -a; //~ manual_isolate_lowest_one | ||
| let _ = -a & a; //~ manual_isolate_lowest_one | ||
| let _ = a & a.wrapping_neg(); //~ manual_isolate_lowest_one | ||
| } | ||
|
|
||
| fn no_lint_different_operand(a: i32, c: i32) { | ||
| // Different operands — must not lint. | ||
| let _ = a & c.wrapping_neg(); | ||
| let _ = a & -c; | ||
| } | ||
|
|
||
| fn no_lint_macros(a: u32) { | ||
| macro_rules! same { | ||
| ($x:expr) => { | ||
| $x & $x.wrapping_neg() | ||
| }; | ||
| } | ||
| same!(a); | ||
| external!($a & $a.wrapping_neg()); | ||
| with_span!(span; a & a.wrapping_neg()); | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,41 @@ | ||
| error: manually reimplementing `isolate_lowest_one` | ||
| --> tests/ui/manual_isolate_lowest_one.rs:8:13 | ||
| | | ||
| LL | let _ = a & a.wrapping_neg(); | ||
| | ^^^^^^^^^^^^^^^^^^^^ help: consider using `.isolate_lowest_one()`: `a.isolate_lowest_one()` | ||
| | | ||
| = note: `-D clippy::manual-isolate-lowest-one` implied by `-D warnings` | ||
| = help: to override `-D warnings` add `#[allow(clippy::manual_isolate_lowest_one)]` | ||
|
|
||
| error: manually reimplementing `isolate_lowest_one` | ||
| --> tests/ui/manual_isolate_lowest_one.rs:9:13 | ||
| | | ||
| LL | let _ = a.wrapping_neg() & a; | ||
| | ^^^^^^^^^^^^^^^^^^^^ help: consider using `.isolate_lowest_one()`: `a.isolate_lowest_one()` | ||
|
|
||
| error: manually reimplementing `isolate_lowest_one` | ||
| --> tests/ui/manual_isolate_lowest_one.rs:10:13 | ||
| | | ||
| LL | let _ = b & b.wrapping_neg(); | ||
| | ^^^^^^^^^^^^^^^^^^^^ help: consider using `.isolate_lowest_one()`: `b.isolate_lowest_one()` | ||
|
|
||
| error: manually reimplementing `isolate_lowest_one` | ||
| --> tests/ui/manual_isolate_lowest_one.rs:14:13 | ||
| | | ||
| LL | let _ = a & -a; | ||
| | ^^^^^^ help: consider using `.isolate_lowest_one()`: `a.isolate_lowest_one()` | ||
|
|
||
| error: manually reimplementing `isolate_lowest_one` | ||
| --> tests/ui/manual_isolate_lowest_one.rs:15:13 | ||
| | | ||
| LL | let _ = -a & a; | ||
| | ^^^^^^ help: consider using `.isolate_lowest_one()`: `a.isolate_lowest_one()` | ||
|
|
||
| error: manually reimplementing `isolate_lowest_one` | ||
| --> tests/ui/manual_isolate_lowest_one.rs:16:13 | ||
| | | ||
| LL | let _ = a & a.wrapping_neg(); | ||
| | ^^^^^^^^^^^^^^^^^^^^ help: consider using `.isolate_lowest_one()`: `a.isolate_lowest_one()` | ||
|
|
||
| error: aborting due to 6 previous errors | ||
|
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
just a semantic edge case worth guarding against, this accepts expressions with side effects, which can silently change the behavior of the program, for example this shouldn't be linted:
linting here is wrong as this would evaluate twice while
next_i32().isolate_lowest_one()would only evaluate once.View changes since the review