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

Commit e339c48

Browse files
committed
Merge #42
42: Add flag to only apply certain lints/errors' suggestions r=killercup a=oli-obk cc #36
2 parents 8e8c826 + 2a3258f commit e339c48

File tree

10 files changed

+62
-3642
lines changed

10 files changed

+62
-3642
lines changed

src/diagnostics.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
pub struct Diagnostic {
77
/// The primary error message.
88
pub message: String,
9-
code: Option<DiagnosticCode>,
9+
pub code: Option<DiagnosticCode>,
1010
/// "error: internal compiler error", "error", "warning", "note", "help".
1111
level: String,
1212
pub spans: Vec<DiagnosticSpan>,
@@ -70,9 +70,9 @@ struct DiagnosticSpanMacroExpansion {
7070
}
7171

7272
#[derive(Deserialize, Debug)]
73-
struct DiagnosticCode {
73+
pub struct DiagnosticCode {
7474
/// The code itself.
75-
code: String,
75+
pub code: String,
7676
/// An explanation for the code.
7777
explanation: Option<String>,
7878
}

src/lib.rs

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
extern crate serde_derive;
33
extern crate serde_json;
44

5+
use std::collections::HashSet;
6+
57
pub mod diagnostics;
68
use diagnostics::{Diagnostic, DiagnosticSpan};
79

@@ -106,7 +108,18 @@ fn collect_span(span: &DiagnosticSpan) -> Option<Replacement> {
106108
})
107109
}
108110

109-
pub fn collect_suggestions(diagnostic: &Diagnostic) -> Option<Suggestion> {
111+
pub fn collect_suggestions(diagnostic: &Diagnostic, only: &HashSet<String>) -> Option<Suggestion> {
112+
if !only.is_empty() {
113+
if let Some(ref code) = diagnostic.code {
114+
if !only.contains(&code.code) {
115+
// This is not the code we are looking for
116+
return None;
117+
}
118+
} else {
119+
// No code, probably a weird builtin warning/error
120+
return None;
121+
}
122+
}
110123

111124
let snippets = diagnostic.spans
112125
.iter()

src/main.rs

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ use std::fs::File;
1313
use std::io::{Read, Write};
1414
use std::error::Error;
1515
use std::process::Command;
16+
use std::collections::HashSet;
1617

1718
use colored::Colorize;
1819
use clap::{Arg, App};
@@ -48,6 +49,11 @@ macro_rules! flush {
4849
() => (try!(std::io::stdout().flush());)
4950
}
5051

52+
/// A list of `--only` aliases
53+
const ALIASES: &[(&str, &[&str])] = &[
54+
("use", &["E0412"]),
55+
];
56+
5157
fn try_main() -> Result<(), ProgramError> {
5258
let matches = App::new("rustfix")
5359
.about("Automatically apply suggestions made by rustc")
@@ -58,6 +64,10 @@ fn try_main() -> Result<(), ProgramError> {
5864
.arg(Arg::with_name("yolo")
5965
.long("yolo")
6066
.help("Automatically apply all unambiguous suggestions"))
67+
.arg(Arg::with_name("only")
68+
.long("only")
69+
.help("Only show errors or lints with the specific id(s) (comma separated)")
70+
.use_delimiter(true))
6171
.get_matches();
6272

6373
let mut extra_args = Vec::new();
@@ -72,6 +82,20 @@ fn try_main() -> Result<(), ProgramError> {
7282
AutofixMode::None
7383
};
7484

85+
let mut only: HashSet<String> = matches
86+
.values_of("only")
87+
.map_or(HashSet::new(), |values| {
88+
values.map(ToString::to_string).collect()
89+
});
90+
91+
for alias in ALIASES {
92+
if only.remove(alias.0) {
93+
for alias in alias.1 {
94+
only.insert(alias.to_string());
95+
}
96+
}
97+
}
98+
7599
// Get JSON output from rustc...
76100
let json = get_json(&extra_args)?;
77101

@@ -80,7 +104,7 @@ fn try_main() -> Result<(), ProgramError> {
80104
// Convert JSON string (and eat parsing errors)
81105
.flat_map(|line| serde_json::from_str::<CargoMessage>(line))
82106
// One diagnostic line might have multiple suggestions
83-
.filter_map(|cargo_msg| rustfix::collect_suggestions(&cargo_msg.message))
107+
.filter_map(|cargo_msg| rustfix::collect_suggestions(&cargo_msg.message, &only))
84108
.collect();
85109

86110
try!(handle_suggestions(suggestions, mode));

tests/fixtures.rs

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
#[macro_use]
22
extern crate duct;
33

4-
use std::io::{BufReader, BufRead};
4+
use std::io::{BufReader, BufRead, Read};
5+
use std::fs::File;
56

67
#[test]
78
fn fixtures() {
@@ -32,7 +33,6 @@ fn fixtures() {
3233

3334
for entry in tests {
3435
let test = entry.unwrap().path();
35-
let yolo = test.file_name().unwrap() == "yolo";
3636

3737
println!("Running test: {}", test.file_name().unwrap().to_str().unwrap());
3838

@@ -47,11 +47,21 @@ fn fixtures() {
4747
println!("Running cargo clippy to obtain suggestions");
4848

4949
let manifest = format!("--manifest-path={}", &manifest[1..manifest.len() - 1]);
50-
let cmd = if yolo {
51-
cmd!("cargo", "run", manifest, "--bin", "rustfix", "--quiet", "--", "--clippy", "--yolo")
52-
} else {
53-
cmd!("cargo", "run", manifest, "--bin", "rustfix", "--quiet", "--", "--clippy")
54-
};
50+
let mut args = vec![
51+
"run".to_owned(),
52+
manifest,
53+
"--bin".to_owned(),
54+
"rustfix".to_owned(),
55+
"--quiet".to_owned(),
56+
"--".to_owned(),
57+
"--clippy".to_owned(),
58+
];
59+
if let Ok(mut file) = File::open(test.join("args.txt")) {
60+
let mut extra_args = String::new();
61+
file.read_to_string(&mut extra_args).unwrap();
62+
args.extend(extra_args.split_whitespace().map(|s| s.to_string()));
63+
}
64+
let cmd = duct::cmd("cargo", args);
5565
cmd.dir(&dir)
5666
.stdin(test.join("input.txt"))
5767
.stdout(dir.join("output.txt"))

tests/tests/libui-rs/simple/args.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
--only transmute_ptr_to_ref,new_without_default
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
--only transmute_ptr_to_ref,new_without_default

tests/tests/libui-rs/yolo/args.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
--yolo --only transmute_ptr_to_ref,new_without_default

0 commit comments

Comments
 (0)