Skip to content
This repository was archived by the owner on Nov 24, 2023. It is now read-only.

Commit a2538c5

Browse files
authored
Merge pull request #155 from pietroalbini/multiple-solutions
Allow multiple solutions in a suggestion
2 parents f99e56d + 4595c3b commit a2538c5

9 files changed

+129
-169
lines changed

src/lib.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -184,7 +184,7 @@ pub fn collect_suggestions<S: ::std::hash::BuildHasher>(
184184
})
185185
.filter_map(collect_span)
186186
.collect();
187-
if replacements.len() == 1 {
187+
if replacements.len() >= 1 {
188188
Some(Solution {
189189
message: child.message.clone(),
190190
replacements,

tests/edge-cases/skip-multi-option-lints.fixed.rs

-5
This file was deleted.

tests/edge-cases/skip-multi-option-lints.json

-100
This file was deleted.

tests/edge-cases/skip-multi-option-lints.rs

-5
This file was deleted.

tests/edge_cases.rs

-12
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
use std::collections::{HashSet};
2+
3+
fn main() {
4+
let _: HashSet<()>;
5+
}
+114
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
1+
{
2+
"message": "unused imports: `HashMap`, `VecDeque`",
3+
"code": {
4+
"code": "unused_imports",
5+
"explanation": null
6+
},
7+
"level": "warning",
8+
"spans": [
9+
{
10+
"file_name": "src/main.rs",
11+
"byte_start": 23,
12+
"byte_end": 30,
13+
"line_start": 1,
14+
"line_end": 1,
15+
"column_start": 24,
16+
"column_end": 31,
17+
"is_primary": true,
18+
"text": [
19+
{
20+
"text": "use std::collections::{HashMap, HashSet, VecDeque};",
21+
"highlight_start": 24,
22+
"highlight_end": 31
23+
}
24+
],
25+
"label": null,
26+
"suggested_replacement": null,
27+
"suggestion_applicability": null,
28+
"expansion": null
29+
},
30+
{
31+
"file_name": "src/main.rs",
32+
"byte_start": 41,
33+
"byte_end": 49,
34+
"line_start": 1,
35+
"line_end": 1,
36+
"column_start": 42,
37+
"column_end": 50,
38+
"is_primary": true,
39+
"text": [
40+
{
41+
"text": "use std::collections::{HashMap, HashSet, VecDeque};",
42+
"highlight_start": 42,
43+
"highlight_end": 50
44+
}
45+
],
46+
"label": null,
47+
"suggested_replacement": null,
48+
"suggestion_applicability": null,
49+
"expansion": null
50+
}
51+
],
52+
"children": [
53+
{
54+
"message": "#[warn(unused_imports)] on by default",
55+
"code": null,
56+
"level": "note",
57+
"spans": [],
58+
"children": [],
59+
"rendered": null
60+
},
61+
{
62+
"message": "remove the unused imports",
63+
"code": null,
64+
"level": "help",
65+
"spans": [
66+
{
67+
"file_name": "src/main.rs",
68+
"byte_start": 23,
69+
"byte_end": 32,
70+
"line_start": 1,
71+
"line_end": 1,
72+
"column_start": 24,
73+
"column_end": 33,
74+
"is_primary": true,
75+
"text": [
76+
{
77+
"text": "use std::collections::{HashMap, HashSet, VecDeque};",
78+
"highlight_start": 24,
79+
"highlight_end": 33
80+
}
81+
],
82+
"label": null,
83+
"suggested_replacement": "",
84+
"suggestion_applicability": "MachineApplicable",
85+
"expansion": null
86+
},
87+
{
88+
"file_name": "src/main.rs",
89+
"byte_start": 39,
90+
"byte_end": 49,
91+
"line_start": 1,
92+
"line_end": 1,
93+
"column_start": 40,
94+
"column_end": 50,
95+
"is_primary": true,
96+
"text": [
97+
{
98+
"text": "use std::collections::{HashMap, HashSet, VecDeque};",
99+
"highlight_start": 40,
100+
"highlight_end": 50
101+
}
102+
],
103+
"label": null,
104+
"suggested_replacement": "",
105+
"suggestion_applicability": "MachineApplicable",
106+
"expansion": null
107+
}
108+
],
109+
"children": [],
110+
"rendered": null
111+
}
112+
],
113+
"rendered": "warning: unused imports: `HashMap`, `VecDeque`\n --> src/main.rs:1:24\n |\n1 | use std::collections::{HashMap, HashSet, VecDeque};\n | ^^^^^^^ ^^^^^^^^\n |\n = note: #[warn(unused_imports)] on by default\nhelp: remove the unused imports\n |\n1 | use std::collections::{HashSet};\n | -- --\n\n"
114+
}
+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
use std::collections::{HashMap, HashSet, VecDeque};
2+
3+
fn main() {
4+
let _: HashSet<()>;
5+
}

tests/parse_and_replace.rs

+4-46
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,6 @@ mod fixmode {
2929

3030
mod settings {
3131
// can be set as env var to debug
32-
pub const CHECK_JSON: &str = "RUSTFIX_TEST_CHECK_JSON";
33-
pub const RECORD_JSON: &str = "RUSTFIX_TEST_RECORD_JSON";
3432
pub const RECORD_FIXED_RUST: &str = "RUSTFIX_TEST_RECORD_FIXED_RUST";
3533
}
3634

@@ -62,20 +60,6 @@ fn compile(file: &Path, mode: &str) -> Result<Output, Error> {
6260
Ok(res)
6361
}
6462

65-
fn compile_and_get_json_errors(file: &Path, mode: &str) -> Result<String, Error> {
66-
let res = compile(file, mode)?;
67-
let stderr = String::from_utf8(res.stderr)?;
68-
69-
match res.status.code() {
70-
Some(0) | Some(1) | Some(101) => Ok(stderr),
71-
_ => Err(format_err!(
72-
"failed with status {:?}: {}",
73-
res.status.code(),
74-
stderr
75-
)),
76-
}
77-
}
78-
7963
fn compiles_without_errors(file: &Path, mode: &str) -> Result<(), Error> {
8064
let res = compile(file, mode)?;
8165

@@ -122,7 +106,8 @@ fn diff(expected: &str, actual: &str) -> String {
122106
write!(
123107
&mut res,
124108
"differences found (+ == actual, - == expected):\n"
125-
).unwrap();
109+
)
110+
.unwrap();
126111
different = true;
127112
}
128113
for diff in diff.lines() {
@@ -149,39 +134,12 @@ fn test_rustfix_with_file<P: AsRef<Path>>(file: P, mode: &str) -> Result<(), Err
149134

150135
debug!("next up: {:?}", file);
151136
let code = read_file(file).context(format!("could not read {}", file.display()))?;
152-
let errors = compile_and_get_json_errors(file, mode)
153-
.context(format!("could compile {}", file.display()))?;
137+
let errors = read_file(&json_file)
138+
.with_context(|_| format!("could not load json suggestions for {}", file.display()))?;
154139
let suggestions =
155140
rustfix::get_suggestions_from_json(&errors, &HashSet::new(), filter_suggestions)
156141
.context("could not load suggestions")?;
157142

158-
if std::env::var(settings::RECORD_JSON).is_ok() {
159-
use std::io::Write;
160-
let mut recorded_json = fs::File::create(&file.with_extension("recorded.json")).context(
161-
format!("could not create recorded.json for {}", file.display()),
162-
)?;
163-
recorded_json.write_all(errors.as_bytes())?;
164-
}
165-
166-
if std::env::var(settings::CHECK_JSON).is_ok() {
167-
let expected_json = read_file(&json_file).context(format!(
168-
"could not load json fixtures for {}",
169-
file.display()
170-
))?;
171-
let expected_suggestions =
172-
rustfix::get_suggestions_from_json(&expected_json, &HashSet::new(), filter_suggestions)
173-
.context("could not load expected suggesitons")?;
174-
175-
ensure!(
176-
expected_suggestions == suggestions,
177-
"got unexpected suggestions from clippy:\n{}",
178-
diff(
179-
&format!("{:?}", expected_suggestions),
180-
&format!("{:?}", suggestions)
181-
)
182-
);
183-
}
184-
185143
let fixed = apply_suggestions(&code, &suggestions)
186144
.context(format!("could not apply suggestions to {}", file.display()))?;
187145

0 commit comments

Comments
 (0)