Skip to content

Commit

Permalink
Implemented is_digit_simply lint
Browse files Browse the repository at this point in the history
Continues rust-lang#6399
  • Loading branch information
bengsparks committed Dec 24, 2020
1 parent 02399f4 commit c8aa0ea
Show file tree
Hide file tree
Showing 6 changed files with 101 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -1878,6 +1878,7 @@ Released 2018-09-13
[`invalid_regex`]: https://rust-lang.github.io/rust-clippy/master/index.html#invalid_regex
[`invalid_upcast_comparisons`]: https://rust-lang.github.io/rust-clippy/master/index.html#invalid_upcast_comparisons
[`invisible_characters`]: https://rust-lang.github.io/rust-clippy/master/index.html#invisible_characters
[`is_digit_simplify`]: https://rust-lang.github.io/rust-clippy/master/index.html#is_digit_simplify
[`items_after_statements`]: https://rust-lang.github.io/rust-clippy/master/index.html#items_after_statements
[`iter_cloned_collect`]: https://rust-lang.github.io/rust-clippy/master/index.html#iter_cloned_collect
[`iter_next_loop`]: https://rust-lang.github.io/rust-clippy/master/index.html#iter_next_loop
Expand Down
6 changes: 6 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,12 @@ path = "src/main.rs"
name = "clippy-driver"
path = "src/driver.rs"

[target.'cfg(NOT_A_PLATFORM)'.dependencies]
rustc_driver = { path = "/home/benji/pull-requests/rust/compiler/rustc_driver" }
rustc_errors = { path = "/home/benji/pull-requests/rust/compiler/rustc_errors" }
rustc_interface = { path = "/home/benji/pull-requests/rust/compiler/rustc_interface" }
rustc_middle = { path = "/home/benji/pull-requests/rust/compiler/rustc_middle" }

[dependencies]
# begin automatic update
clippy_lints = { version = "0.0.212", path = "clippy_lints" }
Expand Down
23 changes: 23 additions & 0 deletions clippy_lints/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,29 @@ license = "MIT OR Apache-2.0"
keywords = ["clippy", "lint", "plugin"]
edition = "2018"

[target.'cfg(NOT_A_PLATFORM)'.dependencies]
rustc_ast = { path = "/home/benji/pull-requests/rust/compiler/rustc_ast" }
rustc_ast_pretty = { path = "/home/benji/pull-requests/rust/compiler/rustc_ast_pretty" }
rustc_attr = { path = "/home/benji/pull-requests/rust/compiler/rustc_attr" }
rustc_data_structures = { path = "/home/benji/pull-requests/rust/compiler/rustc_data_structures" }
rustc_driver = { path = "/home/benji/pull-requests/rust/compiler/rustc_driver" }
rustc_errors = { path = "/home/benji/pull-requests/rust/compiler/rustc_errors" }
rustc_hir = { path = "/home/benji/pull-requests/rust/compiler/rustc_hir" }
rustc_hir_pretty = { path = "/home/benji/pull-requests/rust/compiler/rustc_hir_pretty" }
rustc_index = { path = "/home/benji/pull-requests/rust/compiler/rustc_index" }
rustc_infer = { path = "/home/benji/pull-requests/rust/compiler/rustc_infer" }
rustc_lexer = { path = "/home/benji/pull-requests/rust/compiler/rustc_lexer" }
rustc_lint = { path = "/home/benji/pull-requests/rust/compiler/rustc_lint" }
rustc_middle = { path = "/home/benji/pull-requests/rust/compiler/rustc_middle" }
rustc_mir = { path = "/home/benji/pull-requests/rust/compiler/rustc_mir" }
rustc_parse = { path = "/home/benji/pull-requests/rust/compiler/rustc_parse" }
rustc_parse_format = { path = "/home/benji/pull-requests/rust/compiler/rustc_parse_format" }
rustc_session = { path = "/home/benji/pull-requests/rust/compiler/rustc_session" }
rustc_span = { path = "/home/benji/pull-requests/rust/compiler/rustc_span" }
rustc_target = { path = "/home/benji/pull-requests/rust/compiler/rustc_target" }
rustc_trait_selection = { path = "/home/benji/pull-requests/rust/compiler/rustc_trait_selection" }
rustc_typeck = { path = "/home/benji/pull-requests/rust/compiler/rustc_typeck" }

[dependencies]
cargo_metadata = "0.12"
if_chain = "1.0.0"
Expand Down
62 changes: 62 additions & 0 deletions clippy_lints/src/is_digit_simplify.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
use crate::utils::{is_integer_const, span_lint_and_sugg, unsext};
use if_chain::if_chain;
use rustc_errors::Applicability;
use rustc_hir::*;
use rustc_lint::{LateContext, LateLintPass};
use rustc_middle::ty::{self};
use rustc_session::{declare_lint_pass, declare_tool_lint};

declare_clippy_lint! {
/// Finds usages of is_digit that can be replaced with is_ascii_digit or is_ascii_hexdigit.
///
/// **Why is this bad?**
///
/// **Known problems:** None.
///
/// **Example:**
///
/// ```rust
/// // c.is_digit(10)
/// // c.is_digit(16)
/// ```
/// Use instead:
/// ```rust
/// c.is_ascii_digit()
/// c.is_ascii_hexdigit()
/// ```
pub IS_DIGIT_SIMPLIFY,
style,
"Finds usages of is_digit that can be replaced with is_ascii_digit or is_ascii_hexdigit"
}

declare_lint_pass!(IsDigitSimplify => [IS_DIGIT_SIMPLIFY]);

impl LateLintPass<'_> for IsDigitSimplify {
fn check_expr(&mut self, cx: &LateContext<'_>, expr: &Expr<'_>) {
if_chain! {
// calling is_digit
if let ExprKind::MethodCall(path, span, args, _) = &expr.kind;
if path.ident.name == sym!(is_digit);

// on a char
if args.len() == 2;
if let ty::Char = cx.typeck_results().expr_ty(&args[0]).kind();

// with an integral parameter
if let ty::Int(ity) = cx.typeck_results().expr_ty(&args[1]).kind();
then {
let messages = if is_integer_const(cx, &args[1], unsext(cx.tcx, 10, *ity)) {
Some(("is_digit with decimal radix", "consider using is_ascii_digit"))
} else if is_integer_const(cx, &args[1], unsext(cx.tcx, 16, *ity)) {
Some(("is_digit with hexadecimal radix", "consider using is_ascii_hexdigit"))
} else {
None
};

if let Some((reason, sugg)) = messages {
span_lint_and_sugg(cx, IS_DIGIT_SIMPLIFY, *span, reason, "try", String::from(sugg), Applicability::MachineApplicable)
}
}
}
}
}
4 changes: 4 additions & 0 deletions clippy_lints/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -224,6 +224,7 @@ mod inherent_to_string;
mod inline_fn_without_body;
mod int_plus_one;
mod integer_division;
mod is_digit_simplify;
mod items_after_statements;
mod large_const_arrays;
mod large_enum_variant;
Expand Down Expand Up @@ -641,6 +642,7 @@ pub fn register_plugins(store: &mut rustc_lint::LintStore, sess: &Session, conf:
&inline_fn_without_body::INLINE_FN_WITHOUT_BODY,
&int_plus_one::INT_PLUS_ONE,
&integer_division::INTEGER_DIVISION,
&is_digit_simplify::IS_DIGIT_SIMPLIFY,
&items_after_statements::ITEMS_AFTER_STATEMENTS,
&large_const_arrays::LARGE_CONST_ARRAYS,
&large_enum_variant::LARGE_ENUM_VARIANT,
Expand Down Expand Up @@ -1436,6 +1438,7 @@ pub fn register_plugins(store: &mut rustc_lint::LintStore, sess: &Session, conf:
LintId::of(&inherent_to_string::INHERENT_TO_STRING_SHADOW_DISPLAY),
LintId::of(&inline_fn_without_body::INLINE_FN_WITHOUT_BODY),
LintId::of(&int_plus_one::INT_PLUS_ONE),
LintId::of(&is_digit_simplify::IS_DIGIT_SIMPLIFY),
LintId::of(&large_const_arrays::LARGE_CONST_ARRAYS),
LintId::of(&large_enum_variant::LARGE_ENUM_VARIANT),
LintId::of(&len_zero::COMPARISON_TO_EMPTY),
Expand Down Expand Up @@ -1673,6 +1676,7 @@ pub fn register_plugins(store: &mut rustc_lint::LintStore, sess: &Session, conf:
LintId::of(&functions::RESULT_UNIT_ERR),
LintId::of(&if_let_some_result::IF_LET_SOME_RESULT),
LintId::of(&inherent_to_string::INHERENT_TO_STRING),
LintId::of(&is_digit_simplify::IS_DIGIT_SIMPLIFY),
LintId::of(&len_zero::COMPARISON_TO_EMPTY),
LintId::of(&len_zero::LEN_WITHOUT_IS_EMPTY),
LintId::of(&len_zero::LEN_ZERO),
Expand Down
5 changes: 5 additions & 0 deletions tests/ui/is_digit_simplify.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
#![warn(clippy::is_digit_simplify)]

fn main() {
// test code goes here
}

0 comments on commit c8aa0ea

Please sign in to comment.