mirror of
https://github.com/rust-lang/rust-clippy
synced 2024-11-10 23:24:24 +00:00
Updated from_str_radix_10 sugg to be slightly smarter and ran bless
This commit is contained in:
parent
d36fe85569
commit
bf55aee7b1
3 changed files with 33 additions and 6 deletions
|
@ -1,9 +1,12 @@
|
||||||
use if_chain::if_chain;
|
use if_chain::if_chain;
|
||||||
use rustc_errors::Applicability;
|
use rustc_errors::Applicability;
|
||||||
use rustc_hir::*;
|
use rustc_hir::{def, Expr, ExprKind, PrimTy, QPath, TyKind};
|
||||||
use rustc_lint::{LateContext, LateLintPass};
|
use rustc_lint::{LateContext, LateLintPass};
|
||||||
|
use rustc_middle::ty::Ty;
|
||||||
use rustc_session::{declare_lint_pass, declare_tool_lint};
|
use rustc_session::{declare_lint_pass, declare_tool_lint};
|
||||||
|
use rustc_span::symbol::sym;
|
||||||
|
|
||||||
|
use crate::utils::is_type_diagnostic_item;
|
||||||
use crate::utils::span_lint_and_sugg;
|
use crate::utils::span_lint_and_sugg;
|
||||||
use crate::utils::sugg::Sugg;
|
use crate::utils::sugg::Sugg;
|
||||||
|
|
||||||
|
@ -40,8 +43,7 @@ impl LateLintPass<'tcx> for FromStrRadix10 {
|
||||||
fn check_expr(&mut self, cx: &LateContext<'tcx>, exp: &Expr<'tcx>) {
|
fn check_expr(&mut self, cx: &LateContext<'tcx>, exp: &Expr<'tcx>) {
|
||||||
if_chain! {
|
if_chain! {
|
||||||
if let ExprKind::Call(maybe_path, arguments) = &exp.kind;
|
if let ExprKind::Call(maybe_path, arguments) = &exp.kind;
|
||||||
if let ExprKind::Path(qpath) = &maybe_path.kind;
|
if let ExprKind::Path(QPath::TypeRelative(ty, pathseg)) = &maybe_path.kind;
|
||||||
if let QPath::TypeRelative(ty, pathseg) = &qpath;
|
|
||||||
|
|
||||||
// check if the first part of the path is some integer primitive
|
// check if the first part of the path is some integer primitive
|
||||||
if let TyKind::Path(ty_qpath) = &ty.kind;
|
if let TyKind::Path(ty_qpath) = &ty.kind;
|
||||||
|
@ -59,9 +61,20 @@ impl LateLintPass<'tcx> for FromStrRadix10 {
|
||||||
if let rustc_ast::ast::LitKind::Int(10, _) = lit.node;
|
if let rustc_ast::ast::LitKind::Int(10, _) = lit.node;
|
||||||
|
|
||||||
then {
|
then {
|
||||||
|
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]
|
||||||
|
};
|
||||||
|
|
||||||
let sugg = Sugg::hir_with_applicability(
|
let sugg = Sugg::hir_with_applicability(
|
||||||
cx,
|
cx,
|
||||||
&arguments[0],
|
expr,
|
||||||
"<string>",
|
"<string>",
|
||||||
&mut Applicability::MachineApplicable
|
&mut Applicability::MachineApplicable
|
||||||
).maybe_par();
|
).maybe_par();
|
||||||
|
@ -79,3 +92,8 @@ impl LateLintPass<'tcx> for FromStrRadix10 {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// 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)
|
||||||
|
}
|
||||||
|
|
|
@ -35,6 +35,9 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
|
||||||
let string = "300";
|
let string = "300";
|
||||||
i32::from_str_radix(string, 10)?;
|
i32::from_str_radix(string, 10)?;
|
||||||
|
|
||||||
|
let stringier = "400".to_string();
|
||||||
|
i32::from_str_radix(&stringier, 10)?;
|
||||||
|
|
||||||
// none of these should trigger the lint
|
// none of these should trigger the lint
|
||||||
u16::from_str_radix("20", 3)?;
|
u16::from_str_radix("20", 3)?;
|
||||||
i32::from_str_radix("45", 12)?;
|
i32::from_str_radix("45", 12)?;
|
||||||
|
|
|
@ -28,7 +28,7 @@ error: this call to `from_str_radix` can be replaced with a call to `str::parse`
|
||||||
--> $DIR/from_str_radix_10.rs:32:5
|
--> $DIR/from_str_radix_10.rs:32:5
|
||||||
|
|
|
|
||||||
LL | u16::from_str_radix(&("10".to_owned() + "5"), 10)?;
|
LL | u16::from_str_radix(&("10".to_owned() + "5"), 10)?;
|
||||||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `(&("10".to_owned() + "5")).parse::<u16>()`
|
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `(("10".to_owned() + "5")).parse::<u16>()`
|
||||||
|
|
||||||
error: this call to `from_str_radix` can be replaced with a call to `str::parse`
|
error: this call to `from_str_radix` can be replaced with a call to `str::parse`
|
||||||
--> $DIR/from_str_radix_10.rs:33:5
|
--> $DIR/from_str_radix_10.rs:33:5
|
||||||
|
@ -42,5 +42,11 @@ error: this call to `from_str_radix` can be replaced with a call to `str::parse`
|
||||||
LL | i32::from_str_radix(string, 10)?;
|
LL | i32::from_str_radix(string, 10)?;
|
||||||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `string.parse::<i32>()`
|
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `string.parse::<i32>()`
|
||||||
|
|
||||||
error: aborting due to 7 previous errors
|
error: this call to `from_str_radix` can be replaced with a call to `str::parse`
|
||||||
|
--> $DIR/from_str_radix_10.rs:39:5
|
||||||
|
|
|
||||||
|
LL | i32::from_str_radix(&stringier, 10)?;
|
||||||
|
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `stringier.parse::<i32>()`
|
||||||
|
|
||||||
|
error: aborting due to 8 previous errors
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue