Skip to content

Commit

Permalink
from_str_radix_10 : Reorder checks
Browse files Browse the repository at this point in the history
  • Loading branch information
Jarcho committed Jul 5, 2024
1 parent 3fcac2c commit 9f59e9a
Showing 1 changed file with 10 additions and 9 deletions.
19 changes: 10 additions & 9 deletions clippy_lints/src/from_str_radix_10.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,22 +47,23 @@ impl<'tcx> LateLintPass<'tcx> for FromStrRadix10 {
fn check_expr(&mut self, cx: &LateContext<'tcx>, exp: &Expr<'tcx>) {
if let ExprKind::Call(maybe_path, [src, radix]) = &exp.kind
&& let ExprKind::Path(QPath::TypeRelative(ty, pathseg)) = &maybe_path.kind
// do not lint in constant context, because the suggestion won't work.
// NB: keep this check until a new `const_trait_impl` is available and stablized.
&& !in_constant(cx, exp.hir_id)

// check if the second argument is a primitive `10`
&& is_integer_literal(radix, 10)

// check if the second part of the path indeed calls the associated
// function `from_str_radix`
&& pathseg.ident.name.as_str() == "from_str_radix"

// check if the first part of the path is some integer primitive
&& let TyKind::Path(ty_qpath) = &ty.kind
&& let ty_res = cx.qpath_res(ty_qpath, ty.hir_id)
&& let def::Res::PrimTy(prim_ty) = ty_res
&& matches!(prim_ty, PrimTy::Int(_) | PrimTy::Uint(_))

// check if the second part of the path indeed calls the associated
// function `from_str_radix`
&& pathseg.ident.name.as_str() == "from_str_radix"

// check if the second argument is a primitive `10`
&& is_integer_literal(radix, 10)
// do not lint in constant context, because the suggestion won't work.
// NB: keep this check until a new `const_trait_impl` is available and stablized.
&& !in_constant(cx, exp.hir_id)
{
let expr = if let ExprKind::AddrOf(_, _, expr) = &src.kind {
let ty = cx.typeck_results().expr_ty(expr);
Expand Down

0 comments on commit 9f59e9a

Please sign in to comment.