forked from rust-lang/rust
-
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Auto merge of rust-lang#6896 - TaKO8Ki:refactor-lints-in-methods-modu…
…le, r=phansch Refactor lints in methods module This PR refactors methods lints other than the lints I refactored in rust-lang/rust-clippy#6826 and moves some functions to methods/utils.rs. Basically, I follow the instruction described in rust-lang#6680. **For ease of review, I refactored step by step, keeping each commit small.** closes rust-lang/rust-clippy#6886 cc: `@phansch,` `@flip1995,` `@Y-Nak` changelog: Move lints in methods module to their own modules and some function to methods/utils.rs.
- Loading branch information
Showing
27 changed files
with
388 additions
and
333 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
use clippy_utils::diagnostics::span_lint_and_sugg; | ||
use clippy_utils::source::snippet_with_applicability; | ||
use clippy_utils::{method_chain_args, single_segment_path}; | ||
use if_chain::if_chain; | ||
use rustc_errors::Applicability; | ||
use rustc_hir as hir; | ||
use rustc_lint::LateContext; | ||
use rustc_lint::Lint; | ||
use rustc_middle::ty; | ||
use rustc_span::sym; | ||
|
||
/// Wrapper fn for `CHARS_NEXT_CMP` and `CHARS_LAST_CMP` lints. | ||
pub(super) fn check( | ||
cx: &LateContext<'_>, | ||
info: &crate::methods::BinaryExprInfo<'_>, | ||
chain_methods: &[&str], | ||
lint: &'static Lint, | ||
suggest: &str, | ||
) -> bool { | ||
if_chain! { | ||
if let Some(args) = method_chain_args(info.chain, chain_methods); | ||
if let hir::ExprKind::Call(ref fun, ref arg_char) = info.other.kind; | ||
if arg_char.len() == 1; | ||
if let hir::ExprKind::Path(ref qpath) = fun.kind; | ||
if let Some(segment) = single_segment_path(qpath); | ||
if segment.ident.name == sym::Some; | ||
then { | ||
let mut applicability = Applicability::MachineApplicable; | ||
let self_ty = cx.typeck_results().expr_ty_adjusted(&args[0][0]).peel_refs(); | ||
|
||
if *self_ty.kind() != ty::Str { | ||
return false; | ||
} | ||
|
||
span_lint_and_sugg( | ||
cx, | ||
lint, | ||
info.expr.span, | ||
&format!("you should use the `{}` method", suggest), | ||
"like this", | ||
format!("{}{}.{}({})", | ||
if info.eq { "" } else { "!" }, | ||
snippet_with_applicability(cx, args[0][0].span, "..", &mut applicability), | ||
suggest, | ||
snippet_with_applicability(cx, arg_char[0].span, "..", &mut applicability)), | ||
applicability, | ||
); | ||
|
||
return true; | ||
} | ||
} | ||
|
||
false | ||
} |
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,44 @@ | ||
use clippy_utils::diagnostics::span_lint_and_sugg; | ||
use clippy_utils::method_chain_args; | ||
use clippy_utils::source::snippet_with_applicability; | ||
use if_chain::if_chain; | ||
use rustc_ast::ast; | ||
use rustc_errors::Applicability; | ||
use rustc_hir as hir; | ||
use rustc_lint::LateContext; | ||
use rustc_lint::Lint; | ||
|
||
/// Wrapper fn for `CHARS_NEXT_CMP` and `CHARS_LAST_CMP` lints with `unwrap()`. | ||
pub(super) fn check<'tcx>( | ||
cx: &LateContext<'tcx>, | ||
info: &crate::methods::BinaryExprInfo<'_>, | ||
chain_methods: &[&str], | ||
lint: &'static Lint, | ||
suggest: &str, | ||
) -> bool { | ||
if_chain! { | ||
if let Some(args) = method_chain_args(info.chain, chain_methods); | ||
if let hir::ExprKind::Lit(ref lit) = info.other.kind; | ||
if let ast::LitKind::Char(c) = lit.node; | ||
then { | ||
let mut applicability = Applicability::MachineApplicable; | ||
span_lint_and_sugg( | ||
cx, | ||
lint, | ||
info.expr.span, | ||
&format!("you should use the `{}` method", suggest), | ||
"like this", | ||
format!("{}{}.{}('{}')", | ||
if info.eq { "" } else { "!" }, | ||
snippet_with_applicability(cx, args[0][0].span, "..", &mut applicability), | ||
suggest, | ||
c), | ||
applicability, | ||
); | ||
|
||
true | ||
} else { | ||
false | ||
} | ||
} | ||
} |
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,13 @@ | ||
use crate::methods::chars_cmp; | ||
use rustc_lint::LateContext; | ||
|
||
use super::CHARS_LAST_CMP; | ||
|
||
/// Checks for the `CHARS_LAST_CMP` lint. | ||
pub(super) fn check<'tcx>(cx: &LateContext<'tcx>, info: &crate::methods::BinaryExprInfo<'_>) -> bool { | ||
if chars_cmp::check(cx, info, &["chars", "last"], CHARS_LAST_CMP, "ends_with") { | ||
true | ||
} else { | ||
chars_cmp::check(cx, info, &["chars", "next_back"], CHARS_LAST_CMP, "ends_with") | ||
} | ||
} |
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,13 @@ | ||
use crate::methods::chars_cmp_with_unwrap; | ||
use rustc_lint::LateContext; | ||
|
||
use super::CHARS_LAST_CMP; | ||
|
||
/// Checks for the `CHARS_LAST_CMP` lint with `unwrap()`. | ||
pub(super) fn check<'tcx>(cx: &LateContext<'tcx>, info: &crate::methods::BinaryExprInfo<'_>) -> bool { | ||
if chars_cmp_with_unwrap::check(cx, info, &["chars", "last", "unwrap"], CHARS_LAST_CMP, "ends_with") { | ||
true | ||
} else { | ||
chars_cmp_with_unwrap::check(cx, info, &["chars", "next_back", "unwrap"], CHARS_LAST_CMP, "ends_with") | ||
} | ||
} |
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,8 @@ | ||
use rustc_lint::LateContext; | ||
|
||
use super::CHARS_NEXT_CMP; | ||
|
||
/// Checks for the `CHARS_NEXT_CMP` lint. | ||
pub(super) fn check<'tcx>(cx: &LateContext<'tcx>, info: &crate::methods::BinaryExprInfo<'_>) -> bool { | ||
crate::methods::chars_cmp::check(cx, info, &["chars", "next"], CHARS_NEXT_CMP, "starts_with") | ||
} |
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,8 @@ | ||
use rustc_lint::LateContext; | ||
|
||
use super::CHARS_NEXT_CMP; | ||
|
||
/// Checks for the `CHARS_NEXT_CMP` lint with `unwrap()`. | ||
pub(super) fn check<'tcx>(cx: &LateContext<'tcx>, info: &crate::methods::BinaryExprInfo<'_>) -> bool { | ||
crate::methods::chars_cmp_with_unwrap::check(cx, info, &["chars", "next", "unwrap"], CHARS_NEXT_CMP, "starts_with") | ||
} |
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
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
Oops, something went wrong.