-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
157 additions
and
0 deletions.
There are no files selected for viewing
This file contains 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 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 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 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 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,105 @@ | ||
use clippy_config::msrvs::{self, Msrv}; | ||
use clippy_config::Conf; | ||
use clippy_utils::diagnostics::span_lint_and_sugg; | ||
use clippy_utils::source::snippet_with_applicability; | ||
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}; | ||
use rustc_middle::ty; | ||
use rustc_session::impl_lint_pass; | ||
|
||
declare_clippy_lint! { | ||
/// ### What it does | ||
/// Checks for expressions like `31 - x.leading_zeros()` or `x.ilog(2)` which are manual | ||
/// reimplementations of `x.ilog2()` | ||
/// ### Why is this bad? | ||
/// It is easier to read and understand | ||
/// ### Example | ||
/// ```no_run | ||
/// let x: u32 = 5; | ||
/// let log = 31 - x.leading_zeros() | ||
/// ``` | ||
/// Use instead: | ||
/// ```no_run | ||
/// let x: u32 = 5; | ||
/// let log = x.ilog2() | ||
/// ``` | ||
#[clippy::version = "1.82.0"] | ||
pub MANUAL_ILOG2, | ||
complexity, | ||
"manually reimplementing `ilog2`" | ||
} | ||
|
||
pub struct ManualIlog2 { | ||
msrv: Msrv, | ||
} | ||
|
||
impl ManualIlog2 { | ||
#[must_use] | ||
pub fn new(conf: &Conf) -> Self { | ||
Self { | ||
msrv: conf.msrv.clone(), | ||
} | ||
} | ||
} | ||
|
||
impl_lint_pass!(ManualIlog2 => [MANUAL_ILOG2]); | ||
|
||
impl LateLintPass<'_> for ManualIlog2 { | ||
fn check_expr(&mut self, cx: &LateContext<'_>, expr: &Expr<'_>) { | ||
if !self.msrv.meets(msrvs::MANUAL_ILOG2) { | ||
return; | ||
} | ||
let mut applicability = Applicability::MachineApplicable; | ||
|
||
if let ExprKind::Binary(op, left, right) = expr.kind | ||
&& BinOpKind::Sub == op.node | ||
&& let ExprKind::Lit(lit) = left.kind | ||
&& let LitKind::Int(Pu128(val), _) = lit.node | ||
&& let ExprKind::MethodCall(method_name, reciever, _, _) = right.kind | ||
&& method_name.ident.as_str() == "leading_zeros" | ||
{ | ||
let type_ = cx.typeck_results().expr_ty(reciever); | ||
let Some(bit_width) = (match type_.kind() { | ||
ty::Int(itype) => itype.bit_width(), | ||
ty::Uint(itype) => itype.bit_width(), | ||
_ => return, | ||
}) else { | ||
return; | ||
}; | ||
if val == u128::from(bit_width) - 1 { | ||
let sugg = snippet_with_applicability(cx, reciever.span, "..", &mut applicability); | ||
span_lint_and_sugg( | ||
cx, | ||
MANUAL_ILOG2, | ||
expr.span, | ||
"manually reimplementing `ilog2`", | ||
"consider using .ilog2()", | ||
format!("{sugg}.ilog2()"), | ||
applicability, | ||
); | ||
} | ||
} | ||
|
||
if let ExprKind::MethodCall(method_name, reciever, args, _) = expr.kind | ||
&& method_name.ident.as_str() == "ilog" | ||
&& args.len() == 1 | ||
&& let ExprKind::Lit(lit) = args[0].kind | ||
&& let LitKind::Int(Pu128(2), _) = lit.node | ||
&& cx.typeck_results().expr_ty(reciever).is_integral() | ||
{ | ||
let sugg = snippet_with_applicability(cx, reciever.span, "..", &mut applicability); | ||
span_lint_and_sugg( | ||
cx, | ||
MANUAL_ILOG2, | ||
expr.span, | ||
"manually reimplementing `ilog2`", | ||
"consider using .ilog2()", | ||
format!("{sugg}.ilog2()"), | ||
applicability, | ||
); | ||
} | ||
} | ||
} |
This file contains 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,12 @@ | ||
#![warn(clippy::manual_ilog2)] | ||
|
||
fn main() { | ||
let a: u32 = 5; | ||
let _ = a.ilog2(); | ||
let _ = a.ilog2(); | ||
|
||
let b: u64 = 543534; | ||
let _ = b.ilog2(); | ||
|
||
let _ = 64 - b.leading_zeros(); // No lint | ||
} |
This file contains 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,12 @@ | ||
#![warn(clippy::manual_ilog2)] | ||
|
||
fn main() { | ||
let a: u32 = 5; | ||
let _ = 31 - a.leading_zeros(); | ||
let _ = a.ilog(2); | ||
|
||
let b: u64 = 543534; | ||
let _ = 63 - b.leading_zeros(); | ||
|
||
let _ = 64 - b.leading_zeros(); // No lint | ||
} |
This file contains 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:5:13 | ||
| | ||
LL | let _ = 31 - a.leading_zeros(); | ||
| ^^^^^^^^^^^^^^^^^^^^^^ help: consider using .ilog2(): `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:6:13 | ||
| | ||
LL | let _ = a.ilog(2); | ||
| ^^^^^^^^^ help: consider using .ilog2(): `a.ilog2()` | ||
|
||
error: manually reimplementing `ilog2` | ||
--> tests/ui/manual_ilog2.rs:9:13 | ||
| | ||
LL | let _ = 63 - b.leading_zeros(); | ||
| ^^^^^^^^^^^^^^^^^^^^^^ help: consider using .ilog2(): `b.ilog2()` | ||
|
||
error: aborting due to 3 previous errors | ||
|