-
Notifications
You must be signed in to change notification settings - Fork 1.6k
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
Add the from_str_radix_10 lint #6717
Merged
Merged
Changes from 7 commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
b80ac2a
Added boilerplate
booleancoercion 6472939
Implemented majority of from_str_radix_10
booleancoercion a389c02
from_str_radix_10 should be done
booleancoercion 0b31b47
Changed applicability to MaybeIncorrect because of surrounding braces
booleancoercion d1a627a
Ran bless and rustfmt
booleancoercion 9194c11
Fixed doctests that shouldn't have been compiled
booleancoercion 642efab
Fixed typos and updated to matches! where applicable
booleancoercion d36fe85
Made parens addition smarter and added tests with bless
booleancoercion bf55aee
Updated from_str_radix_10 sugg to be slightly smarter and ran bless
booleancoercion c4b8d87
Fixed the known problems section
booleancoercion 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 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,74 @@ | ||
use if_chain::if_chain; | ||
use rustc_errors::Applicability; | ||
use rustc_hir::*; | ||
use rustc_lint::{LateContext, LateLintPass}; | ||
use rustc_session::{declare_lint_pass, declare_tool_lint}; | ||
|
||
use crate::utils::span_lint_and_sugg; | ||
|
||
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. | ||
/// | ||
/// **Known problems:** None. | ||
/// | ||
/// **Example:** | ||
/// | ||
/// ```ignore | ||
/// let input: &str = get_input(); | ||
/// let num = u16::from_str_radix(input, 10)?; | ||
/// ``` | ||
/// Use instead: | ||
/// ```ignore | ||
/// let input: &str = get_input(); | ||
/// let num: u16 = input.parse()?; | ||
/// ``` | ||
pub FROM_STR_RADIX_10, | ||
style, | ||
"from_str_radix with radix 10" | ||
} | ||
|
||
declare_lint_pass!(FromStrRadix10 => [FROM_STR_RADIX_10]); | ||
|
||
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; | ||
if let ExprKind::Path(qpath) = &maybe_path.kind; | ||
if let QPath::TypeRelative(ty, pathseg) = &qpath; | ||
|
||
// 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; | ||
if matches!(prim_ty, PrimTy::Int(_) | PrimTy::Uint(_)); | ||
|
||
// 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"; | ||
|
||
// check if the second argument is a primitive `10` | ||
if arguments.len() == 2; | ||
if let ExprKind::Lit(lit) = &arguments[1].kind; | ||
if let rustc_ast::ast::LitKind::Int(10, _) = lit.node; | ||
|
||
then { | ||
let orig_string = crate::utils::snippet(cx, arguments[0].span, "string"); | ||
span_lint_and_sugg( | ||
cx, | ||
FROM_STR_RADIX_10, | ||
exp.span, | ||
"this call to `from_str_radix` can be replaced with a call to `str::parse`", | ||
"try", | ||
format!("({}).parse()", orig_string), | ||
booleancoercion marked this conversation as resolved.
Show resolved
Hide resolved
|
||
Applicability::MaybeIncorrect | ||
); | ||
} | ||
} | ||
} | ||
} |
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,36 @@ | ||
#![warn(clippy::from_str_radix_10)] | ||
|
||
mod some_mod { | ||
// fake function that shouldn't trigger the lint | ||
pub fn from_str_radix(_: &str, _: u32) -> Result<(), std::num::ParseIntError> { | ||
unimplemented!() | ||
} | ||
} | ||
|
||
// fake function that shouldn't trigger the lint | ||
fn from_str_radix(_: &str, _: u32) -> Result<(), std::num::ParseIntError> { | ||
unimplemented!() | ||
} | ||
|
||
fn main() -> Result<(), Box<dyn std::error::Error>> { | ||
// all of these should trigger the lint | ||
u32::from_str_radix("30", 10)?; | ||
i64::from_str_radix("24", 10)?; | ||
isize::from_str_radix("100", 10)?; | ||
u8::from_str_radix("7", 10)?; | ||
|
||
let string = "300"; | ||
i32::from_str_radix(string, 10)?; | ||
|
||
// none of these should trigger the lint | ||
u16::from_str_radix("20", 3)?; | ||
i32::from_str_radix("45", 12)?; | ||
usize::from_str_radix("10", 16)?; | ||
i128::from_str_radix("10", 13)?; | ||
some_mod::from_str_radix("50", 10)?; | ||
some_mod::from_str_radix("50", 6)?; | ||
from_str_radix("50", 10)?; | ||
from_str_radix("50", 6)?; | ||
|
||
Ok(()) | ||
} |
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,34 @@ | ||
error: this call to `from_str_radix` can be replaced with a call to `str::parse` | ||
--> $DIR/from_str_radix_10.rs:17:5 | ||
| | ||
LL | u32::from_str_radix("30", 10)?; | ||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `("30").parse()` | ||
| | ||
= note: `-D clippy::from-str-radix-10` implied by `-D warnings` | ||
|
||
error: this call to `from_str_radix` can be replaced with a call to `str::parse` | ||
--> $DIR/from_str_radix_10.rs:18:5 | ||
| | ||
LL | i64::from_str_radix("24", 10)?; | ||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `("24").parse()` | ||
|
||
error: this call to `from_str_radix` can be replaced with a call to `str::parse` | ||
--> $DIR/from_str_radix_10.rs:19:5 | ||
| | ||
LL | isize::from_str_radix("100", 10)?; | ||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `("100").parse()` | ||
|
||
error: this call to `from_str_radix` can be replaced with a call to `str::parse` | ||
--> $DIR/from_str_radix_10.rs:20:5 | ||
| | ||
LL | u8::from_str_radix("7", 10)?; | ||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `("7").parse()` | ||
|
||
error: this call to `from_str_radix` can be replaced with a call to `str::parse` | ||
--> $DIR/from_str_radix_10.rs:23:5 | ||
| | ||
LL | i32::from_str_radix(string, 10)?; | ||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `(string).parse()` | ||
|
||
error: aborting due to 5 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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think
style
is OK, as I don't expect any false positives here.