Skip to content

Commit 849a38a

Browse files
committed
Avoid suggesting traits multiple times.
This is clearly useless, the user doesn't need to know that they could implement/import `foo::bar::Baz` 4 times. Fixes rust-lang#21405.
1 parent ada312f commit 849a38a

File tree

2 files changed

+24
-0
lines changed

2 files changed

+24
-0
lines changed

src/librustc_typeck/check/method/suggest.rs

+2
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,7 @@ fn suggest_traits_to_import<'a, 'tcx>(fcx: &FnCtxt<'a, 'tcx>,
145145
if !valid_out_of_scope_traits.is_empty() {
146146
let mut candidates = valid_out_of_scope_traits;
147147
candidates.sort();
148+
candidates.dedup();
148149
let msg = format!(
149150
"methods from traits can only be called if the trait is in scope; \
150151
the following {traits_are} implemented but not in scope, \
@@ -172,6 +173,7 @@ fn suggest_traits_to_import<'a, 'tcx>(fcx: &FnCtxt<'a, 'tcx>,
172173
if candidates.len() > 0 {
173174
// sort from most relevant to least relevant
174175
candidates.sort_by(|a, b| a.cmp(b).reverse());
176+
candidates.dedup();
175177

176178
let msg = format!(
177179
"methods from traits can only be called if the trait is implemented and in scope; \
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
// Copyright 2015 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
// issue #21405
12+
13+
fn foo<F>(f: F) where F: FnMut(usize) {}
14+
15+
fn main() {
16+
foo(|s| s.is_empty());
17+
//~^ ERROR does not implement any method
18+
//~^^ HELP #1: `core::slice::SliceExt`
19+
//~^^^ HELP #2: `core::str::StrExt`
20+
//~^^^^ HELP #3: `collections::slice::SliceExt`
21+
//~^^^^^ HELP #4: `collections::str::StrExt`
22+
}

0 commit comments

Comments
 (0)