Skip to content

Commit 3887403

Browse files
committed
fixed most of the clippy violations
1 parent bad433e commit 3887403

File tree

6 files changed

+358
-109
lines changed

6 files changed

+358
-109
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
/target

.vscode/launch.json

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
{
2+
// Use IntelliSense to learn about possible attributes.
3+
// Hover to view descriptions of existing attributes.
4+
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
5+
"version": "0.2.0",
6+
"configurations": [
7+
{
8+
"type": "lldb",
9+
"request": "launch",
10+
"name": "Debug executable 'warning'",
11+
"cargo": {
12+
"args": [
13+
"build",
14+
"--bin=warning",
15+
"--package=warning"
16+
],
17+
"filter": {
18+
"name": "warning",
19+
"kind": "bin"
20+
}
21+
},
22+
"args": [],
23+
"cwd": "${workspaceFolder}"
24+
},
25+
{
26+
"type": "lldb",
27+
"request": "launch",
28+
"name": "Debug unit tests in executable 'warning'",
29+
"cargo": {
30+
"args": [
31+
"test",
32+
"--no-run",
33+
"--bin=warning",
34+
"--package=warning"
35+
],
36+
"filter": {
37+
"name": "warning",
38+
"kind": "bin"
39+
}
40+
},
41+
"args": [],
42+
"cwd": "${workspaceFolder}"
43+
}
44+
]
45+
}

Cargo.lock

Lines changed: 17 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,3 +7,4 @@ edition = "2021"
77

88
[dependencies]
99
cargo_metadata = "*"
10+
snailquote = "*"

src/main.rs

Lines changed: 117 additions & 109 deletions
Original file line numberDiff line numberDiff line change
@@ -22,19 +22,29 @@ fn markup(source: &[u8], map: Vec<Ran>) -> Vec<u8> {
2222
for m in &map {
2323
// deal with the element
2424
if m.start <= i && i < m.end && i == m.start {
25-
output.extend(format!("/*@start: {}*/", m.name).as_bytes());
25+
output.extend(format!("/*{}*/", m.name).as_bytes());
2626
}
2727
if m.end == i {
28-
match m.suggestion.as_str() {
29-
"None" => output.extend(format!("/*@end: {}*/", m.name).as_bytes()),
30-
_ => output.extend(
31-
format!(
32-
"/*@end: {} suggestion: {} note: {}*/",
33-
m.name, m.suggestion, m.note
34-
)
35-
.as_bytes(),
36-
),
37-
}
28+
output.extend(
29+
format!(
30+
"/*\n{}{}{}*/",
31+
m.name,
32+
if m.suggestion == "None" {
33+
"".to_string()
34+
} else {
35+
format!(
36+
"\nsuggestion: {}",
37+
m.suggestion.replace("\\n", "\n").replace('\"', "")
38+
)
39+
},
40+
if m.note == "None" {
41+
"".to_string()
42+
} else {
43+
format!("\nnote: {}", m.note.replace("\\n", "\n").replace('\"', ""))
44+
}
45+
)
46+
.as_bytes(),
47+
)
3848
}
3949
}
4050
output.push(*c);
@@ -45,121 +55,119 @@ fn markup(source: &[u8], map: Vec<Ran>) -> Vec<u8> {
4555
// Run cargo clippy to generate warnings from "foo.rs" into temporary "foo.rs.1" files
4656
fn main() {
4757
remove_previously_generated_files();
48-
let mut command = Command::new("cargo")
49-
.args(["clippy", "--message-format=json"])
58+
let args = vec!["clippy", "--message-format=json"];
59+
if std::env::args().len() > 1 && args[1] == "--fix" {
60+
vec!["clippy", "--message-format=json", "--fix", "--allow-dirty", "--broken-code"];
61+
}
62+
if let Ok(mut command) = Command::new("cargo")
63+
.args(args)
5064
.stdout(Stdio::piped())
5165
.spawn()
52-
.unwrap();
53-
// if let Some(reader)
54-
let reader = std::io::BufReader::new(command.stdout.take().unwrap());
55-
56-
let mut map = HashMap::new();
57-
for message in cargo_metadata::Message::parse_stream(reader) {
58-
if let Message::CompilerMessage(msg) = message.expect("something wrong with the message") {
59-
// dbg!(&msg);
60-
for s in msg.message.spans {
61-
let x = s.byte_start as usize;
62-
let y = s.byte_end as usize;
63-
let r = Ran {
64-
// name: msg.message.message.clone(),
65-
name: format!(
66-
"#[{:?}({})",
67-
msg.message.level,
68-
msg.message
69-
.code
70-
.clone()
71-
.expect("something wrong with the message code")
72-
.code
73-
),
74-
start: x,
75-
end: y,
76-
suggestion: format!("{:?}", s.suggested_replacement),
77-
note: format!("{:?}", sub_messages(&msg.message.children)),
78-
};
79-
let filename = s.file_name;
80-
if !map.contains_key(&filename) {
81-
let v = vec![r];
82-
map.insert(filename, v);
83-
} else {
84-
if let Some(v) = map.get_mut(&filename) {
85-
v.push(r);
86-
} else {
87-
println!("something wrong with the filename");
66+
{
67+
if let Some(take) = command.stdout.take() {
68+
let reader = std::io::BufReader::new(take);
69+
let mut map: HashMap<String, Vec<Ran>> = HashMap::new();
70+
for message in cargo_metadata::Message::parse_stream(reader).flatten() {
71+
if let Message::CompilerMessage(msg) = message {
72+
for s in msg.message.spans {
73+
if let Ok(x) = usize::try_from(s.byte_start) {
74+
if let Ok(y) = usize::try_from(s.byte_end) {
75+
if let Some(message_code) = &msg.message.code {
76+
let r = Ran {
77+
name: format!(
78+
"#[{:?}({})",
79+
msg.message.level,
80+
message_code.clone().code
81+
),
82+
start: x,
83+
end: y,
84+
suggestion: format!("{:?}", s.suggested_replacement),
85+
note: format!("{:?}", sub_messages(&msg.message.children)),
86+
};
87+
dbg!(&r);
88+
let filename = s.file_name;
89+
match map.get_mut(&filename) {
90+
Some(v) => v.push(r),
91+
None => {
92+
let v = vec![r];
93+
map.insert(filename, v);
94+
}
95+
}
96+
}
97+
}
98+
}
8899
}
89100
}
90101
}
102+
for file in map.keys() {
103+
if let Ok(source) = read_to_string(file) {
104+
if let Some(v) = map.get(file) {
105+
let output = markup(source.as_bytes(), v.to_vec());
106+
let path = PathBuf::from(file);
107+
if let Some(p_path) = path.parent() {
108+
if let Some(stem) = path.file_stem() {
109+
let file_name = p_path.join(PathBuf::from(format!(
110+
"{}.rs.diagnostics",
111+
stem.to_string_lossy()
112+
)));
113+
println!("Marked warning(s) into {:?}", &file_name);
114+
if let Some(p) = file_name.parent() {
115+
if !p.exists() {
116+
std::fs::create_dir(p).ok();
117+
}
118+
}
119+
if let Ok(content) = std::str::from_utf8(&output) {
120+
std::fs::write(&file_name, content).ok();
121+
}
122+
}
123+
}
124+
}
125+
}
126+
}
127+
command.wait().ok();
91128
}
92129
}
93-
for file in map.keys() {
94-
let source = read_to_string(file)
95-
.ok()
96-
.expect("sth wrong with reading the file into a string");
97-
let v = map
98-
.get(file)
99-
.expect("sth wrong with getting a file from the map");
100-
let output = markup(source.as_bytes(), v.to_vec());
101-
let path = PathBuf::from(file);
102-
let file_name = path
103-
.parent()
104-
.expect("something wrong with the parent of the path")
105-
.join(format!(
106-
"{}.rs.diagnostics",
107-
path.file_stem()
108-
.expect("something wrong with the file stem")
109-
.to_string_lossy()
110-
));
111-
println!("Marked warning(s) into {:?}", &file_name);
112-
if !file_name
113-
.parent()
114-
.expect("sth wrong with the parent of the file_name")
115-
.exists()
116-
{
117-
std::fs::create_dir(
118-
file_name
119-
.parent()
120-
.expect("something with creating the folder"),
121-
)
122-
.ok();
123-
}
124-
std::fs::write(
125-
&file_name,
126-
std::str::from_utf8(&output).expect("sth wrong with converting output into utf8 str"),
127-
)
128-
.ok();
129-
}
130-
let _output = command.wait().expect("Couldn't get cargo's exit status");
131130
}
132131

133-
fn sub_messages(children: &Vec<Diagnostic>) -> String {
132+
fn sub_messages(children: &[Diagnostic]) -> String {
134133
children
135-
.into_iter()
136-
.map(|x| x.message.to_owned())
134+
.iter()
135+
.map(|x| {
136+
if let Some(rendered) = &x.rendered {
137+
format!("{}: {}", &x.message, &rendered)
138+
} else {
139+
x.message.to_owned()
140+
}
141+
})
137142
.collect::<Vec<String>>()
138143
.join("\n")
139144
}
140145

141146
fn remove_previously_generated_files() {
142-
let command = Command::new("find")
147+
if let Ok(command) = Command::new("find")
143148
.args([".", "-name", "*.rs.diagnostics"])
144149
.stdout(Stdio::piped())
145150
.spawn()
146-
.expect("rm diagnostics files failed");
147-
let output = command
148-
.wait_with_output()
149-
.expect("failed to aquire programm output")
150-
.stdout;
151-
if !output.is_empty() {
152-
println!("Removed previously generated warning files")
151+
{
152+
if let Ok(output) = command.wait_with_output() {
153+
if !output.stdout.is_empty() {
154+
println!("Removed previously generated warning files")
155+
}
156+
if let Ok(s) = String::from_utf8(output.stdout) {
157+
s.split('\n').for_each(|tmp| {
158+
if let Ok(mut command) = Command::new("rm")
159+
.args(["-f", tmp])
160+
.stdout(Stdio::piped())
161+
.spawn()
162+
{
163+
if let Ok(w) = command.wait() {
164+
if !w.success() {
165+
println!("wait not successful");
166+
}
167+
}
168+
}
169+
});
170+
}
171+
}
153172
}
154-
String::from_utf8(output)
155-
.expect("programm output was not valid utf-8")
156-
.split('\n')
157-
.for_each(|tmp| {
158-
let mut command = Command::new("rm")
159-
.args(["-f", tmp])
160-
.stdout(Stdio::piped())
161-
.spawn()
162-
.expect("failed to remove the file");
163-
command.wait().expect("problem with file deletion");
164-
});
165173
}

0 commit comments

Comments
 (0)