-
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
Refactor: arrange lints in methods
module
#6826
Conversation
r? @phansch (rust-highfive has picked a reviewer for you, use r? to override) |
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 really like these refactors! Feel free to @ me again, when you're ready for a review =)
@phansch |
methods
module
81d4dbe
to
62a1119
Compare
You should probably split into a new PR every 10 or so lints for the reviewers' sanity 😄 |
@camsteffen |
methods
modulemethods
module
@phansch |
@TaKO8Ki great! I will try to finish the review in the next 12 hours so that we don't run in too many conflicts with other PRs |
} | ||
} | ||
} | ||
|
||
fn fn_header_equals(expected: hir::FnHeader, actual: hir::FnHeader) -> bool { |
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.
If those utils functions are used in more than one lint, please move it to a methods/utils.rs
module. If it is only used in one lint, move it to the lint module. If it is only used in mod.rs
, just leave it here.
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.
A quick search would suggest, that this function should stay in mod.rs
, but e.g. derefs_to_slice
is used in multiple lint files and should therefore be moved to methods/utils.rs
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.
You're right. I think I should move some functions such as derefs_to_slice
to methods/utils.rs
, however this PR is too big. So, I'm going to fix this in a new PR. ref comment above
let self_ty = cx.typeck_results().expr_ty_adjusted(&args[0]); | ||
if args.len() == 1 && method_call.ident.name == sym::clone { | ||
lint_clone_on_copy(cx, expr, &args[0], self_ty); | ||
lint_clone_on_ref_ptr(cx, expr, &args[0]); | ||
clone_on_copy::check(cx, expr, &args[0], self_ty); | ||
clone_on_ref_ptr::check(cx, expr, &args[0]); | ||
} | ||
if args.len() == 1 && method_call.ident.name == sym!(to_string) { | ||
inefficient_to_string::lint(cx, expr, &args[0], self_ty); | ||
inefficient_to_string::check(cx, expr, &args[0], self_ty); | ||
} | ||
|
||
if let Some(fn_def_id) = cx.typeck_results().type_dependent_def_id(expr.hir_id) { | ||
if match_def_path(cx, fn_def_id, &paths::PUSH_STR) { | ||
lint_single_char_push_string(cx, expr, args); | ||
single_char_push_string::check(cx, expr, args); | ||
} else if match_def_path(cx, fn_def_id, &paths::INSERT_STR) { | ||
lint_single_char_insert_string(cx, expr, args); | ||
single_char_insert_string::check(cx, expr, args); | ||
} | ||
} | ||
|
||
match self_ty.kind() { | ||
ty::Ref(_, ty, _) if *ty.kind() == ty::Str => { | ||
for &(method, pos) in &PATTERN_METHODS { | ||
if method_call.ident.name.as_str() == method && args.len() > pos { | ||
lint_single_char_pattern(cx, expr, &args[pos]); | ||
single_char_pattern::check(cx, expr, &args[pos]); | ||
} | ||
} | ||
}, | ||
ty::Ref(..) if method_call.ident.name == sym::into_iter => { | ||
lint_into_iter(cx, expr, self_ty, *method_span); | ||
into_iter_on_ref::check(cx, expr, self_ty, *method_span); | ||
}, | ||
_ => (), | ||
} |
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.
These lines seem to do too many tasks.
I think checks that decide whether the lint is triggered should be moved to each module if duplications are not so much.
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.
Thank you for your suggestions! I think so too. However, this PR is too big and I have to commit many changes in order to move them to each module. So I'm going to do it in a new PR 👍 ref: #6826 (comment)
☔ The latest upstream changes (presumably #6881) made this pull request unmergeable. Please resolve the merge conflicts. |
for all reviewers This PR is too big. So, if your suggestions are big, I'm going to accept them in a new PR. ref #6826 (comment) |
@TaKO8Ki sounds good to me! (And sorry for the merge conflict (-.-) ) |
I agree that we should keep further improvements out of this PR 👍 |
I agree that too. |
…to their own modules
d502a56
to
99f8607
Compare
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.
Left some comments unrelated to this refactoring that can be fixed in later PRs
pub(super) fn check<'tcx>( | ||
cx: &LateContext<'tcx>, | ||
expr: &'tcx hir::Expr<'_>, | ||
_filter_args: &'tcx [hir::Expr<'_>], |
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.
We should review all new functions for unused arguments
} else if is_type_diagnostic_item(cx, cx.typeck_results().expr_ty(caller_expr), sym::vec_type) | ||
|| matches!( | ||
&cx.typeck_results().expr_ty(caller_expr).peel_refs().kind(), | ||
ty::Array(_, _) | ||
) |
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.
This condition (not the body) could probably be extracted into its own function
// lint if caller of skip is an Iterator | ||
if match_trait_method(cx, expr, &paths::ITERATOR) { | ||
if let [caller, n] = skip_args { | ||
let hint = format!(".nth({})", snippet(cx, n.span, "..")); |
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.
No need to have this as a separate variable
} | ||
|
||
/// This checks whether a given type is known to implement Debug. | ||
fn has_debug_impl<'tcx>(ty: Ty<'tcx>, cx: &LateContext<'tcx>) -> bool { |
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.
Might be worth moving to utils
|
||
pub(super) fn check(cx: &LateContext<'_>, expr: &hir::Expr<'_>, args: &[hir::Expr<'_>]) { | ||
let obj_ty = cx.typeck_results().expr_ty(&args[0]).peel_refs(); | ||
if is_type_diagnostic_item(cx, obj_ty, sym::string_type) { |
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.
Returning early here or using if_chain
would reduce some indentation in a lot of lints, like here.
@bors r+ This is a huge step forward, thanks! |
📌 Commit 83a9553 has been approved by |
☀️ Test successful - checks-action_dev_test, checks-action_remark_test, checks-action_test |
…nsch Refactor lints in methods module This PR refactors methods lints other than the lints I refactored in #6826 and moves some functions to methods/utils.rs. Basically, I follow the instruction described in #6680. **For ease of review, I refactored step by step, keeping each commit small.** closes #6886 cc: `@phansch,` `@flip1995,` `@Y-Nak` changelog: Move lints in methods module to their own modules and some function to methods/utils.rs.
This PR arranges methods lints so that they can be accessed more easily.
Basically, I refactored them following the instruction described in #6680.
changelog: Move lints in methods module into their own modules.