-
Notifications
You must be signed in to change notification settings - Fork 1.8k
feat(manual_ilog2): new lint #15865
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
Merged
+209
−0
Merged
feat(manual_ilog2): new lint #15865
Changes from all commits
Commits
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,115 @@ | ||
| use clippy_config::Conf; | ||
| use clippy_utils::diagnostics::span_lint_and_sugg; | ||
| use clippy_utils::msrvs::{self, Msrv}; | ||
| use clippy_utils::source::snippet_with_applicability; | ||
| use clippy_utils::{is_from_proc_macro, sym}; | ||
| use rustc_ast::LitKind; | ||
| use rustc_data_structures::packed::Pu128; | ||
| use rustc_errors::Applicability; | ||
| use rustc_hir::{BinOpKind, Expr, ExprKind}; | ||
| use rustc_lint::{LateContext, LateLintPass, LintContext}; | ||
| use rustc_middle::ty; | ||
| use rustc_session::impl_lint_pass; | ||
|
|
||
| declare_clippy_lint! { | ||
| /// ### What it does | ||
| /// Checks for expressions like `N - x.leading_zeros()` (where `N` is one less than bit width | ||
| /// of `x`) or `x.ilog(2)`, which are manual reimplementations of `x.ilog2()` | ||
| /// | ||
| /// ### Why is this bad? | ||
| /// Manual reimplementations of `ilog2` increase code complexity for little benefit. | ||
| /// | ||
| /// ### Example | ||
| /// ```no_run | ||
| /// let x: u32 = 5; | ||
| /// let log = 31 - x.leading_zeros(); | ||
| /// let log = x.ilog(2); | ||
| /// ``` | ||
| /// Use instead: | ||
| /// ```no_run | ||
| /// let x: u32 = 5; | ||
| /// let log = x.ilog2(); | ||
| /// let log = x.ilog2(); | ||
| /// ``` | ||
| #[clippy::version = "1.93.0"] | ||
| pub MANUAL_ILOG2, | ||
| pedantic, | ||
| "manually reimplementing `ilog2`" | ||
| } | ||
|
|
||
| pub struct ManualIlog2 { | ||
| msrv: Msrv, | ||
| } | ||
|
|
||
| impl ManualIlog2 { | ||
| pub fn new(conf: &Conf) -> Self { | ||
| Self { msrv: conf.msrv } | ||
| } | ||
| } | ||
|
|
||
| impl_lint_pass!(ManualIlog2 => [MANUAL_ILOG2]); | ||
|
|
||
| impl LateLintPass<'_> for ManualIlog2 { | ||
| fn check_expr<'tcx>(&mut self, cx: &LateContext<'tcx>, expr: &Expr<'tcx>) { | ||
| if expr.span.in_external_macro(cx.sess().source_map()) { | ||
| return; | ||
| } | ||
|
|
||
| match expr.kind { | ||
| // `BIT_WIDTH - 1 - n.leading_zeros()` | ||
| ExprKind::Binary(op, left, right) | ||
| if left.span.eq_ctxt(right.span) | ||
| && op.node == BinOpKind::Sub | ||
| && let ExprKind::Lit(lit) = left.kind | ||
| && let LitKind::Int(Pu128(val), _) = lit.node | ||
| && let ExprKind::MethodCall(leading_zeros, recv, [], _) = right.kind | ||
| && leading_zeros.ident.name == sym::leading_zeros | ||
| && let ty = cx.typeck_results().expr_ty(recv) | ||
| && let Some(bit_width) = match ty.kind() { | ||
| ty::Uint(uint_ty) => uint_ty.bit_width(), | ||
| ty::Int(_) => { | ||
| // On non-positive integers, `ilog2` would panic, which might be a sign that the author does | ||
| // in fact want to calculate something different, so stay on the safer side and don't | ||
| // suggest anything. | ||
| return; | ||
| }, | ||
| _ => return, | ||
| } | ||
| && val == u128::from(bit_width) - 1 | ||
| && self.msrv.meets(cx, msrvs::ILOG2) | ||
| && !is_from_proc_macro(cx, expr) => | ||
| { | ||
| emit(cx, recv, expr); | ||
| }, | ||
|
|
||
| // `n.ilog(2)` | ||
| ExprKind::MethodCall(ilog, recv, [two], _) | ||
| if expr.span.eq_ctxt(two.span) | ||
| && ilog.ident.name == sym::ilog | ||
| && let ExprKind::Lit(lit) = two.kind | ||
| && let LitKind::Int(Pu128(2), _) = lit.node | ||
| && cx.typeck_results().expr_ty_adjusted(recv).is_integral() | ||
| /* no need to check MSRV here, as `ilog` and `ilog2` were introduced simultaneously */ | ||
| && !is_from_proc_macro(cx, expr) => | ||
| { | ||
| emit(cx, recv, expr); | ||
| }, | ||
|
|
||
| _ => {}, | ||
| } | ||
| } | ||
| } | ||
|
|
||
| fn emit(cx: &LateContext<'_>, recv: &Expr<'_>, full_expr: &Expr<'_>) { | ||
| let mut app = Applicability::MachineApplicable; | ||
| let recv = snippet_with_applicability(cx, recv.span, "_", &mut app); | ||
| span_lint_and_sugg( | ||
| cx, | ||
| MANUAL_ILOG2, | ||
| full_expr.span, | ||
| "manually reimplementing `ilog2`", | ||
| "try", | ||
| format!("{recv}.ilog2()"), | ||
| app, | ||
| ); | ||
| } | ||
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,32 @@ | ||
| //@aux-build:proc_macros.rs | ||
| #![warn(clippy::manual_ilog2)] | ||
| #![allow(clippy::unnecessary_operation)] | ||
|
|
||
| use proc_macros::{external, with_span}; | ||
|
|
||
| fn foo(a: u32, b: u64) { | ||
| a.ilog2(); //~ manual_ilog2 | ||
| a.ilog2(); //~ manual_ilog2 | ||
|
|
||
| b.ilog2(); //~ manual_ilog2 | ||
| 64 - b.leading_zeros(); // No lint because manual ilog2 is `BIT_WIDTH - 1 - x.leading_zeros()` | ||
|
|
||
| // don't lint when macros are involved | ||
| macro_rules! two { | ||
| () => { | ||
| 2 | ||
| }; | ||
| }; | ||
|
|
||
| macro_rules! thirty_one { | ||
| () => { | ||
| 31 | ||
| }; | ||
| }; | ||
|
|
||
| a.ilog(two!()); | ||
| thirty_one!() - a.leading_zeros(); | ||
|
|
||
| external!($a.ilog(2)); | ||
| with_span!(span; a.ilog(2)); | ||
| } |
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,32 @@ | ||
| //@aux-build:proc_macros.rs | ||
| #![warn(clippy::manual_ilog2)] | ||
| #![allow(clippy::unnecessary_operation)] | ||
|
|
||
| use proc_macros::{external, with_span}; | ||
|
|
||
| fn foo(a: u32, b: u64) { | ||
| 31 - a.leading_zeros(); //~ manual_ilog2 | ||
| a.ilog(2); //~ manual_ilog2 | ||
|
|
||
| 63 - b.leading_zeros(); //~ manual_ilog2 | ||
| 64 - b.leading_zeros(); // No lint because manual ilog2 is `BIT_WIDTH - 1 - x.leading_zeros()` | ||
|
|
||
| // don't lint when macros are involved | ||
| macro_rules! two { | ||
| () => { | ||
| 2 | ||
| }; | ||
| }; | ||
|
|
||
| macro_rules! thirty_one { | ||
| () => { | ||
| 31 | ||
| }; | ||
| }; | ||
|
|
||
| a.ilog(two!()); | ||
| thirty_one!() - a.leading_zeros(); | ||
|
|
||
| external!($a.ilog(2)); | ||
| with_span!(span; a.ilog(2)); | ||
| } |
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,23 @@ | ||
| error: manually reimplementing `ilog2` | ||
| --> tests/ui/manual_ilog2.rs:8:5 | ||
| | | ||
| LL | 31 - a.leading_zeros(); | ||
| | ^^^^^^^^^^^^^^^^^^^^^^ help: try: `a.ilog2()` | ||
| | | ||
| = note: `-D clippy::manual-ilog2` implied by `-D warnings` | ||
| = help: to override `-D warnings` add `#[allow(clippy::manual_ilog2)]` | ||
|
|
||
| error: manually reimplementing `ilog2` | ||
| --> tests/ui/manual_ilog2.rs:9:5 | ||
| | | ||
| LL | a.ilog(2); | ||
| | ^^^^^^^^^ help: try: `a.ilog2()` | ||
|
|
||
| error: manually reimplementing `ilog2` | ||
| --> tests/ui/manual_ilog2.rs:11:5 | ||
| | | ||
| LL | 63 - b.leading_zeros(); | ||
| | ^^^^^^^^^^^^^^^^^^^^^^ help: try: `b.ilog2()` | ||
|
|
||
| error: aborting due to 3 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.