Skip to content

Commit 9b8b734

Browse files
authored
Merge pull request rust-lang#108 from jrlusby/skip_multiples
Skip suggestions with multiple options Fixes rust-lang#78
2 parents 114bdc7 + e6f1ace commit 9b8b734

5 files changed

+123
-3
lines changed

src/lib.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -166,13 +166,13 @@ pub fn collect_suggestions<S: ::std::hash::BuildHasher>(
166166
.iter()
167167
.filter_map(|child| {
168168
let replacements: Vec<_> = child.spans.iter().filter_map(collect_span).collect();
169-
if replacements.is_empty() {
170-
None
171-
} else {
169+
if replacements.len() == 1 {
172170
Some(Solution {
173171
message: child.message.clone(),
174172
replacements,
175173
})
174+
} else {
175+
None
176176
}
177177
})
178178
.collect();
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
fn main() {
2+
let xs = vec![String::from("foo")];
3+
let d: &Display = &xs;
4+
println!("{}", d);
5+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
{
2+
"message": "cannot find type `Display` in this scope",
3+
"code": {
4+
"code": "E0412",
5+
"explanation": "\nThe type name used is not in scope.\n\nErroneous code examples:\n\n```compile_fail,E0412\nimpl Something {} // error: type name `Something` is not in scope\n\n// or:\n\ntrait Foo {\n fn bar(N); // error: type name `N` is not in scope\n}\n\n// or:\n\nfn foo(x: T) {} // type name `T` is not in scope\n```\n\nTo fix this error, please verify you didn't misspell the type name, you did\ndeclare it or imported it into the scope. Examples:\n\n```\nstruct Something;\n\nimpl Something {} // ok!\n\n// or:\n\ntrait Foo {\n type N;\n\n fn bar(_: Self::N); // ok!\n}\n\n// or:\n\nfn foo<T>(x: T) {} // ok!\n```\n\nAnother case that causes this error is when a type is imported into a parent\nmodule. To fix this, you can follow the suggestion and use File directly or\n`use super::File;` which will import the types from the parent namespace. An\nexample that causes this error is below:\n\n```compile_fail,E0412\nuse std::fs::File;\n\nmod foo {\n fn some_function(f: File) {}\n}\n```\n\n```\nuse std::fs::File;\n\nmod foo {\n // either\n use super::File;\n // or\n // use std::fs::File;\n fn foo(f: File) {}\n}\n# fn main() {} // don't insert it for us; that'll break imports\n```\n"
6+
},
7+
"level": "error",
8+
"spans": [
9+
{
10+
"file_name": "./tests/everything/skip-multi-option-lints.rs",
11+
"byte_start": 64,
12+
"byte_end": 71,
13+
"line_start": 3,
14+
"line_end": 3,
15+
"column_start": 13,
16+
"column_end": 20,
17+
"is_primary": true,
18+
"text": [
19+
{
20+
"text": " let d: &Display = &xs;",
21+
"highlight_start": 13,
22+
"highlight_end": 20
23+
}
24+
],
25+
"label": "not found in this scope",
26+
"suggested_replacement": null,
27+
"expansion": null
28+
}
29+
],
30+
"children": [
31+
{
32+
"message": "possible candidates are found in other modules, you can import them into scope",
33+
"code": null,
34+
"level": "help",
35+
"spans": [
36+
{
37+
"file_name": "./tests/everything/skip-multi-option-lints.rs",
38+
"byte_start": 0,
39+
"byte_end": 0,
40+
"line_start": 1,
41+
"line_end": 1,
42+
"column_start": 1,
43+
"column_end": 1,
44+
"is_primary": true,
45+
"text": [
46+
{
47+
"text": "fn main() {",
48+
"highlight_start": 1,
49+
"highlight_end": 1
50+
}
51+
],
52+
"label": null,
53+
"suggested_replacement": "use std::fmt::Display;\n\n",
54+
"suggestion_applicability": "Unspecified",
55+
"expansion": null
56+
},
57+
{
58+
"file_name": "./tests/everything/skip-multi-option-lints.rs",
59+
"byte_start": 0,
60+
"byte_end": 0,
61+
"line_start": 1,
62+
"line_end": 1,
63+
"column_start": 1,
64+
"column_end": 1,
65+
"is_primary": true,
66+
"text": [
67+
{
68+
"text": "fn main() {",
69+
"highlight_start": 1,
70+
"highlight_end": 1
71+
}
72+
],
73+
"label": null,
74+
"suggested_replacement": "use std::path::Display;\n\n",
75+
"suggestion_applicability": "Unspecified",
76+
"expansion": null
77+
}
78+
],
79+
"children": [],
80+
"rendered": null
81+
}
82+
],
83+
"rendered": "error[E0412]: cannot find type `Display` in this scope\n --> ./tests/everything/skip-multi-option-lints.rs:3:13\n |\n3 | let d: &Display = &xs;\n | ^^^^^^^ not found in this scope\nhelp: possible candidates are found in other modules, you can import them into scope\n |\n1 | use std::fmt::Display;\n |\n1 | use std::path::Display;\n |\n\n"
84+
}
85+
{
86+
"message": "aborting due to previous error",
87+
"code": null,
88+
"level": "error",
89+
"spans": [],
90+
"children": [],
91+
"rendered": "error: aborting due to previous error\n\n"
92+
}
93+
{
94+
"message": "For more information about this error, try `rustc --explain E0412`.",
95+
"code": null,
96+
"level": "",
97+
"spans": [],
98+
"children": [],
99+
"rendered": "For more information about this error, try `rustc --explain E0412`.\n"
100+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
fn main() {
2+
let xs = vec![String::from("foo")];
3+
let d: &Display = &xs;
4+
println!("{}", d);
5+
}

tests/edge_cases.rs

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
extern crate rustfix;
2+
use std::collections::HashSet;
3+
use std::fs;
4+
5+
#[test]
6+
fn multiple_fix_options_yield_no_suggestions() {
7+
let json = fs::read_to_string("./tests/edge-cases/skip-multi-option-lints.json").unwrap();
8+
let expected_suggestions = rustfix::get_suggestions_from_json(&json, &HashSet::new()).unwrap();
9+
assert!(expected_suggestions.is_empty());
10+
}

0 commit comments

Comments
 (0)