-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Refactor lints in methods module #6896
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
Merged
bors
merged 19 commits into
rust-lang:master
from
TaKO8Ki:refactor-lints-in-methods-module
Mar 22, 2021
Merged
Changes from all commits
Commits
Show all changes
19 commits
Select commit
Hold shift + click to select a range
94fb2b5
move chars_cmp and chars_next_cmp to its own modules
TaKO8Ki 058d8c8
move chars_cmp_with_unwrap, chars_last_cmp and chars_next_cmp_with_un…
TaKO8Ki c07c046
refactor string_extend_chars: return when obj type is not string
TaKO8Ki e578a53
move derefs_to_slice to methods/utils.rs
TaKO8Ki 1bec8b6
use derefs_to_slice in methods/utils.rs
TaKO8Ki f0a101d
remove a needless variable
TaKO8Ki 0c81311
extract a condition into a function.
TaKO8Ki b6597ee
remove unused arguments
TaKO8Ki f0f7871
fmt
TaKO8Ki 4843084
use clippy_utils::ty::is_type_diagnostic_item
TaKO8Ki 3d9b45d
move single_char_add_str to its own module
TaKO8Ki 62490c4
extract conditions into modules
TaKO8Ki d380769
extract conditions for `from_iter_instead_of_collect` into its own m…
TaKO8Ki 7a7fcc0
extract condition for into_iter_on_ref to its own module
TaKO8Ki 4d1f2bc
extract conditions for single_char_pattern into its own module
TaKO8Ki 27963c8
move chars_last_cmp_with_unwrap to its own module
TaKO8Ki 602bcf3
move get_hint_if_single_char_arg to methods/utils.rs
TaKO8Ki 0edd5f8
remove the use of paths
TaKO8Ki b6a2757
replace crate::methods::utils with super::utils
TaKO8Ki 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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.
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 wonder if
chars_last_cmp::check
andchars_next_cmp::check
should be merged into this because they are almost the same and depend on each other. I think separating them in this way decreases readability and understandability. This also applies tochars_cmp_with_unwrap
.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 agree with you. I'm going to merge
chars_last_cmp
andchars_next_cmp
and mergechars_next_cmp_with_unwrap
andchars_last_cmp_with_unwrap
.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'm going to do it in a new PR.