2021-03-16 00:55:45 +00:00
|
|
|
use clippy_utils::diagnostics::span_lint_and_sugg;
|
2021-03-13 23:01:03 +00:00
|
|
|
use clippy_utils::ty::is_type_diagnostic_item;
|
2021-02-11 00:30:37 +00:00
|
|
|
use if_chain::if_chain;
|
2021-02-11 10:46:11 +00:00
|
|
|
use rustc_errors::Applicability;
|
2021-02-19 17:36:28 +00:00
|
|
|
use rustc_hir::{def, Expr, ExprKind, PrimTy, QPath, TyKind};
|
2021-02-11 10:46:11 +00:00
|
|
|
use rustc_lint::{LateContext, LateLintPass};
|
2021-02-19 17:36:28 +00:00
|
|
|
use rustc_middle::ty::Ty;
|
2021-02-11 10:46:11 +00:00
|
|
|
use rustc_session::{declare_lint_pass, declare_tool_lint};
|
2021-02-19 17:36:28 +00:00
|
|
|
use rustc_span::symbol::sym;
|
2021-02-11 00:30:37 +00:00
|
|
|
|
2021-02-12 22:33:08 +00:00
|
|
|
use crate::utils::sugg::Sugg;
|
2021-02-10 19:47:04 +00:00
|
|
|
|
|
|
|
declare_clippy_lint! {
|
|
|
|
/// **What it does:**
|
|
|
|
/// Checks for function invocations of the form `primitive::from_str_radix(s, 10)`
|
|
|
|
///
|
|
|
|
/// **Why is this bad?**
|
|
|
|
/// This specific common use case can be rewritten as `s.parse::<primitive>()`
|
|
|
|
/// (and in most cases, the turbofish can be removed), which reduces code length
|
|
|
|
/// and complexity.
|
|
|
|
///
|
2021-02-19 20:00:23 +00:00
|
|
|
/// **Known problems:**
|
|
|
|
/// This lint may suggest using (&<expression>).parse() instead of <expression>.parse() directly
|
|
|
|
/// in some cases, which is correct but adds unnecessary complexity to the code.
|
2021-02-11 10:46:11 +00:00
|
|
|
///
|
2021-02-10 19:47:04 +00:00
|
|
|
/// **Example:**
|
|
|
|
///
|
2021-02-11 11:15:06 +00:00
|
|
|
/// ```ignore
|
2021-02-10 19:47:04 +00:00
|
|
|
/// let input: &str = get_input();
|
|
|
|
/// let num = u16::from_str_radix(input, 10)?;
|
|
|
|
/// ```
|
|
|
|
/// Use instead:
|
2021-02-11 11:15:06 +00:00
|
|
|
/// ```ignore
|
2021-02-10 19:47:04 +00:00
|
|
|
/// let input: &str = get_input();
|
|
|
|
/// let num: u16 = input.parse()?;
|
|
|
|
/// ```
|
|
|
|
pub FROM_STR_RADIX_10,
|
|
|
|
style,
|
2021-02-11 00:30:37 +00:00
|
|
|
"from_str_radix with radix 10"
|
2021-02-10 19:47:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
declare_lint_pass!(FromStrRadix10 => [FROM_STR_RADIX_10]);
|
|
|
|
|
2021-02-11 00:30:37 +00:00
|
|
|
impl LateLintPass<'tcx> for FromStrRadix10 {
|
|
|
|
fn check_expr(&mut self, cx: &LateContext<'tcx>, exp: &Expr<'tcx>) {
|
|
|
|
if_chain! {
|
|
|
|
if let ExprKind::Call(maybe_path, arguments) = &exp.kind;
|
2021-02-19 17:36:28 +00:00
|
|
|
if let ExprKind::Path(QPath::TypeRelative(ty, pathseg)) = &maybe_path.kind;
|
2021-02-11 00:30:37 +00:00
|
|
|
|
|
|
|
// check if the first part of the path is some integer primitive
|
|
|
|
if let TyKind::Path(ty_qpath) = &ty.kind;
|
|
|
|
let ty_res = cx.qpath_res(ty_qpath, ty.hir_id);
|
|
|
|
if let def::Res::PrimTy(prim_ty) = ty_res;
|
2021-02-12 09:53:52 +00:00
|
|
|
if matches!(prim_ty, PrimTy::Int(_) | PrimTy::Uint(_));
|
2021-02-11 00:30:37 +00:00
|
|
|
|
|
|
|
// check if the second part of the path indeed calls the associated
|
|
|
|
// function `from_str_radix`
|
|
|
|
if pathseg.ident.name.as_str() == "from_str_radix";
|
|
|
|
|
2021-02-11 10:30:23 +00:00
|
|
|
// check if the second argument is a primitive `10`
|
2021-02-11 00:30:37 +00:00
|
|
|
if arguments.len() == 2;
|
2021-02-11 10:30:23 +00:00
|
|
|
if let ExprKind::Lit(lit) = &arguments[1].kind;
|
|
|
|
if let rustc_ast::ast::LitKind::Int(10, _) = lit.node;
|
2021-02-11 00:30:37 +00:00
|
|
|
|
|
|
|
then {
|
2021-02-19 17:36:28 +00:00
|
|
|
let expr = if let ExprKind::AddrOf(_, _, expr) = &arguments[0].kind {
|
|
|
|
let ty = cx.typeck_results().expr_ty(expr);
|
|
|
|
if is_ty_stringish(cx, ty) {
|
|
|
|
expr
|
|
|
|
} else {
|
|
|
|
&arguments[0]
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
&arguments[0]
|
|
|
|
};
|
|
|
|
|
2021-02-12 22:33:08 +00:00
|
|
|
let sugg = Sugg::hir_with_applicability(
|
|
|
|
cx,
|
2021-02-19 17:36:28 +00:00
|
|
|
expr,
|
2021-02-12 22:33:08 +00:00
|
|
|
"<string>",
|
|
|
|
&mut Applicability::MachineApplicable
|
|
|
|
).maybe_par();
|
|
|
|
|
2021-02-11 00:30:37 +00:00
|
|
|
span_lint_and_sugg(
|
|
|
|
cx,
|
|
|
|
FROM_STR_RADIX_10,
|
|
|
|
exp.span,
|
2021-02-12 09:53:52 +00:00
|
|
|
"this call to `from_str_radix` can be replaced with a call to `str::parse`",
|
2021-02-11 00:30:37 +00:00
|
|
|
"try",
|
2021-02-12 22:33:08 +00:00
|
|
|
format!("{}.parse::<{}>()", sugg, prim_ty.name_str()),
|
2021-02-11 10:42:20 +00:00
|
|
|
Applicability::MaybeIncorrect
|
2021-02-11 00:30:37 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-02-19 17:36:28 +00:00
|
|
|
|
|
|
|
/// Checks if a Ty is `String` or `&str`
|
|
|
|
fn is_ty_stringish(cx: &LateContext<'_>, ty: Ty<'_>) -> bool {
|
|
|
|
is_type_diagnostic_item(cx, ty, sym::string_type) || is_type_diagnostic_item(cx, ty, sym::str)
|
|
|
|
}
|