@@ -4,11 +4,13 @@ use hir::ItemKind;
44use rustc_ast:: Mutability ;
55use rustc_errors:: Applicability ;
66use rustc_hir as hir;
7+ use rustc_middle:: ty:: subst:: InternalSubsts ;
78use rustc_middle:: ty:: { Ref , Ty } ;
89use rustc_session:: lint:: builtin:: RUST_2021_PRELUDE_COLLISIONS ;
910use rustc_span:: symbol:: kw:: Underscore ;
1011use rustc_span:: symbol:: { sym, Ident } ;
1112use rustc_span:: Span ;
13+ use rustc_trait_selection:: infer:: InferCtxtExt ;
1214
1315use crate :: check:: {
1416 method:: probe:: { self , Pick } ,
@@ -206,6 +208,25 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
206208 return ;
207209 }
208210
211+ // For from_iter, check if the type actualy implements FromIterator.
212+ // If we know it does not, we don't need to warn.
213+ if method_name. name == sym:: from_iter {
214+ if let Some ( trait_def_id) = self . tcx . get_diagnostic_item ( sym:: FromIterator ) {
215+ if !self
216+ . infcx
217+ . type_implements_trait (
218+ trait_def_id,
219+ self_ty,
220+ InternalSubsts :: empty ( ) ,
221+ self . param_env ,
222+ )
223+ . may_apply ( )
224+ {
225+ return ;
226+ }
227+ }
228+ }
229+
209230 // No need to lint if this is an inherent method called on a specific type, like `Vec::foo(...)`,
210231 // since such methods take precedence over trait methods.
211232 if matches ! ( pick. kind, probe:: PickKind :: InherentImplPick ) {
0 commit comments