-
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
18 changed files
with
160 additions
and
14 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
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,50 @@ | ||
//! Lint for `c.is_digit(10)` | ||
use super::IS_DIGIT_ASCII_RADIX; | ||
use clippy_utils::{ | ||
consts::constant_full_int, consts::FullInt, diagnostics::span_lint_and_sugg, meets_msrv, msrvs, | ||
source::snippet_with_applicability, | ||
}; | ||
use rustc_errors::Applicability; | ||
use rustc_hir::Expr; | ||
use rustc_lint::LateContext; | ||
use rustc_semver::RustcVersion; | ||
|
||
pub(super) fn check<'tcx>( | ||
cx: &LateContext<'tcx>, | ||
expr: &'tcx Expr<'_>, | ||
self_arg: &'tcx Expr<'_>, | ||
radix: &'tcx Expr<'_>, | ||
msrv: Option<&RustcVersion>, | ||
) { | ||
if !meets_msrv(msrv, &msrvs::IS_ASCII_DIGIT) { | ||
return; | ||
} | ||
|
||
if !cx.typeck_results().expr_ty_adjusted(self_arg).peel_refs().is_char() { | ||
return; | ||
} | ||
|
||
if let Some(radix_val) = constant_full_int(cx, cx.typeck_results(), radix) { | ||
let (num, replacement) = match radix_val { | ||
FullInt::S(10) | FullInt::U(10) => (10, "is_ascii_digit"), | ||
FullInt::S(16) | FullInt::U(16) => (16, "is_ascii_hexdigit"), | ||
_ => return, | ||
}; | ||
let mut applicability = Applicability::MachineApplicable; | ||
|
||
span_lint_and_sugg( | ||
cx, | ||
IS_DIGIT_ASCII_RADIX, | ||
expr.span, | ||
&format!("use of `char::is_digit` with literal radix of {}", num), | ||
"try", | ||
format!( | ||
"{}.{}()", | ||
snippet_with_applicability(cx, self_arg.span, "..", &mut applicability), | ||
replacement | ||
), | ||
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
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,18 @@ | ||
// run-rustfix | ||
|
||
#![warn(clippy::is_digit_ascii_radix)] | ||
|
||
const TEN: u32 = 10; | ||
|
||
fn main() { | ||
let c: char = '6'; | ||
|
||
// Should trigger the lint. | ||
let _ = c.is_ascii_digit(); | ||
let _ = c.is_ascii_hexdigit(); | ||
let _ = c.is_ascii_hexdigit(); | ||
|
||
// Should not trigger the lint. | ||
let _ = c.is_digit(11); | ||
let _ = c.is_digit(TEN); | ||
} |
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,18 @@ | ||
// run-rustfix | ||
|
||
#![warn(clippy::is_digit_ascii_radix)] | ||
|
||
const TEN: u32 = 10; | ||
|
||
fn main() { | ||
let c: char = '6'; | ||
|
||
// Should trigger the lint. | ||
let _ = c.is_digit(10); | ||
let _ = c.is_digit(16); | ||
let _ = c.is_digit(0x10); | ||
|
||
// Should not trigger the lint. | ||
let _ = c.is_digit(11); | ||
let _ = c.is_digit(TEN); | ||
} |
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,22 @@ | ||
error: use of `char::is_digit` with literal radix of 10 | ||
--> $DIR/is_digit_ascii_radix.rs:11:13 | ||
| | ||
LL | let _ = c.is_digit(10); | ||
| ^^^^^^^^^^^^^^ help: try: `c.is_ascii_digit()` | ||
| | ||
= note: `-D clippy::is-digit-ascii-radix` implied by `-D warnings` | ||
|
||
error: use of `char::is_digit` with literal radix of 16 | ||
--> $DIR/is_digit_ascii_radix.rs:12:13 | ||
| | ||
LL | let _ = c.is_digit(16); | ||
| ^^^^^^^^^^^^^^ help: try: `c.is_ascii_hexdigit()` | ||
|
||
error: use of `char::is_digit` with literal radix of 16 | ||
--> $DIR/is_digit_ascii_radix.rs:13:13 | ||
| | ||
LL | let _ = c.is_digit(0x10); | ||
| ^^^^^^^^^^^^^^^^ help: try: `c.is_ascii_hexdigit()` | ||
|
||
error: aborting due to 3 previous errors | ||
|
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 |
---|---|---|
@@ -1,16 +1,16 @@ | ||
error: use of `.to_digit(..).is_some()` | ||
--> $DIR/to_digit_is_some.rs:9:13 | ||
| | ||
LL | let _ = d.to_digit(10).is_some(); | ||
| ^^^^^^^^^^^^^^^^^^^^^^^^ help: try this: `d.is_digit(10)` | ||
LL | let _ = d.to_digit(8).is_some(); | ||
| ^^^^^^^^^^^^^^^^^^^^^^^ help: try this: `d.is_digit(8)` | ||
| | ||
= note: `-D clippy::to-digit-is-some` implied by `-D warnings` | ||
|
||
error: use of `.to_digit(..).is_some()` | ||
--> $DIR/to_digit_is_some.rs:10:13 | ||
| | ||
LL | let _ = char::to_digit(c, 10).is_some(); | ||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try this: `char::is_digit(c, 10)` | ||
LL | let _ = char::to_digit(c, 8).is_some(); | ||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try this: `char::is_digit(c, 8)` | ||
|
||
error: aborting due to 2 previous errors | ||
|