-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Suppress iter_on_empty_collections
if the iterator's concrete type is relied upon
#12823
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
Changes from all commits
9d311b5
3629372
3955bd4
b31625c
7439ecb
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,12 @@ | ||
use std::iter::once; | ||
|
||
use clippy_utils::diagnostics::span_lint_and_sugg; | ||
use clippy_utils::source::snippet; | ||
use clippy_utils::{get_expr_use_or_unification_node, is_res_lang_ctor, path_res, std_or_core}; | ||
|
||
use rustc_errors::Applicability; | ||
use rustc_hir::def_id::DefId; | ||
use rustc_hir::hir_id::HirId; | ||
use rustc_hir::LangItem::{OptionNone, OptionSome}; | ||
use rustc_hir::{Expr, ExprKind, Node}; | ||
use rustc_lint::LateContext; | ||
|
@@ -25,7 +29,29 @@ impl IterType { | |
} | ||
} | ||
|
||
pub(super) fn check(cx: &LateContext<'_>, expr: &Expr<'_>, method_name: &str, recv: &Expr<'_>) { | ||
fn is_arg_ty_unified_in_fn<'tcx>( | ||
cx: &LateContext<'tcx>, | ||
fn_id: DefId, | ||
arg_id: HirId, | ||
args: impl IntoIterator<Item = &'tcx Expr<'tcx>>, | ||
) -> bool { | ||
let fn_sig = cx.tcx.fn_sig(fn_id).instantiate_identity(); | ||
let arg_id_in_args = args.into_iter().position(|e| e.hir_id == arg_id).unwrap(); | ||
let arg_ty_in_args = fn_sig.input(arg_id_in_args).skip_binder(); | ||
|
||
cx.tcx.predicates_of(fn_id).predicates.iter().any(|(clause, _)| { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Maybe an interesting followup (in addition to looking through predicates, doesn't need to be done in this PR) would be to run a type visitor through each parameter type and check if the argument type is in there. I.e. when we have There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Added that as well & added a use of |
||
clause | ||
.as_projection_clause() | ||
.and_then(|p| p.map_bound(|p| p.term.ty()).transpose()) | ||
.is_some_and(|ty| ty.skip_binder() == arg_ty_in_args) | ||
}) || fn_sig | ||
.inputs() | ||
.iter() | ||
.enumerate() | ||
.any(|(i, ty)| i != arg_id_in_args && ty.skip_binder().walk().any(|arg| arg.as_type() == Some(arg_ty_in_args))) | ||
} | ||
|
||
pub(super) fn check<'tcx>(cx: &LateContext<'tcx>, expr: &'tcx Expr<'tcx>, method_name: &str, recv: &'tcx Expr<'tcx>) { | ||
let item = match recv.kind { | ||
ExprKind::Array([]) => None, | ||
ExprKind::Array([e]) => Some(e), | ||
|
@@ -43,6 +69,25 @@ pub(super) fn check(cx: &LateContext<'_>, expr: &Expr<'_>, method_name: &str, re | |
let is_unified = match get_expr_use_or_unification_node(cx.tcx, expr) { | ||
Some((Node::Expr(parent), child_id)) => match parent.kind { | ||
ExprKind::If(e, _, _) | ExprKind::Match(e, _, _) if e.hir_id == child_id => false, | ||
ExprKind::Call( | ||
Expr { | ||
kind: ExprKind::Path(path), | ||
hir_id, | ||
.. | ||
}, | ||
args, | ||
) => cx | ||
.typeck_results() | ||
.qpath_res(path, *hir_id) | ||
.opt_def_id() | ||
.filter(|fn_id| cx.tcx.def_kind(fn_id).is_fn_like()) | ||
.is_some_and(|fn_id| is_arg_ty_unified_in_fn(cx, fn_id, child_id, args)), | ||
ExprKind::MethodCall(_name, recv, args, _span) => is_arg_ty_unified_in_fn( | ||
cx, | ||
cx.typeck_results().type_dependent_def_id(parent.hir_id).unwrap(), | ||
child_id, | ||
once(recv).chain(args.iter()), | ||
), | ||
ExprKind::If(_, _, _) | ||
| ExprKind::Match(_, _, _) | ||
| ExprKind::Closure(_) | ||
|
Uh oh!
There was an error while loading. Please reload this page.