Skip to content

Commit 6de27a0

Browse files
committed
lintcheck: key lints on line start rather than byte start/end
1 parent 780c61f commit 6de27a0

File tree

2 files changed

+28
-25
lines changed

2 files changed

+28
-25
lines changed

lintcheck/src/json.rs

+21-18
Original file line numberDiff line numberDiff line change
@@ -12,21 +12,21 @@ const TRUNCATION_TOTAL_TARGET: usize = 1000;
1212

1313
#[derive(Debug, Deserialize, Serialize)]
1414
struct LintJson {
15-
lint: String,
16-
krate: String,
17-
file_name: String,
18-
byte_pos: (u32, u32),
19-
file_link: String,
15+
/// The lint name e.g. `clippy::bytes_nth`
16+
name: String,
17+
/// The filename and line number e.g. `anyhow-1.0.86/src/error.rs:42`
18+
file_line: String,
19+
file_url: String,
2020
rendered: String,
2121
}
2222

2323
impl LintJson {
2424
fn key(&self) -> impl Ord + '_ {
25-
(self.lint.as_str(), self.file_name.as_str(), self.byte_pos)
25+
(self.name.as_str(), self.file_line.as_str())
2626
}
2727

2828
fn info_text(&self, action: &str) -> String {
29-
format!("{action} `{}` in `{}` at {}", self.lint, self.krate, self.file_link)
29+
format!("{action} `{}` at [`{}`]({})", self.name, self.file_line, self.file_url)
3030
}
3131
}
3232

@@ -36,13 +36,16 @@ pub(crate) fn output(clippy_warnings: Vec<ClippyWarning>) -> String {
3636
.into_iter()
3737
.map(|warning| {
3838
let span = warning.span();
39+
let file_name = span
40+
.file_name
41+
.strip_prefix("target/lintcheck/sources/")
42+
.unwrap_or(&span.file_name);
43+
let file_line = format!("{file_name}:{}", span.line_start);
3944
LintJson {
40-
file_name: span.file_name.clone(),
41-
byte_pos: (span.byte_start, span.byte_end),
42-
krate: warning.krate,
43-
file_link: warning.url,
44-
lint: warning.lint,
45-
rendered: warning.diag.rendered.unwrap(),
45+
name: warning.name,
46+
file_line,
47+
file_url: warning.url,
48+
rendered: warning.diag.rendered.unwrap().trim().to_string(),
4649
}
4750
})
4851
.collect();
@@ -63,7 +66,7 @@ pub(crate) fn diff(old_path: &Path, new_path: &Path, truncate: bool) {
6366
let mut lint_warnings = vec![];
6467

6568
for (name, changes) in &itertools::merge_join_by(old_warnings, new_warnings, |old, new| old.key().cmp(&new.key()))
66-
.chunk_by(|change| change.as_ref().into_left().lint.to_string())
69+
.chunk_by(|change| change.as_ref().into_left().name.clone())
6770
{
6871
let mut added = Vec::new();
6972
let mut removed = Vec::new();
@@ -162,7 +165,7 @@ fn print_warnings(title: &str, warnings: &[LintJson], truncate_after: usize) {
162165
return;
163166
}
164167

165-
print_h3(&warnings[0].lint, title);
168+
print_h3(&warnings[0].name, title);
166169
println!();
167170

168171
let warnings = truncate(warnings, truncate_after);
@@ -171,7 +174,7 @@ fn print_warnings(title: &str, warnings: &[LintJson], truncate_after: usize) {
171174
println!("{}", warning.info_text(title));
172175
println!();
173176
println!("```");
174-
println!("{}", warning.rendered.trim_end());
177+
println!("{}", warning.rendered);
175178
println!("```");
176179
println!();
177180
}
@@ -182,7 +185,7 @@ fn print_changed_diff(changed: &[(LintJson, LintJson)], truncate_after: usize) {
182185
return;
183186
}
184187

185-
print_h3(&changed[0].0.lint, "Changed");
188+
print_h3(&changed[0].0.name, "Changed");
186189
println!();
187190

188191
let changed = truncate(changed, truncate_after);
@@ -191,7 +194,7 @@ fn print_changed_diff(changed: &[(LintJson, LintJson)], truncate_after: usize) {
191194
println!("{}", new.info_text("Changed"));
192195
println!();
193196
println!("```diff");
194-
for change in diff::lines(old.rendered.trim_end(), new.rendered.trim_end()) {
197+
for change in diff::lines(&old.rendered, &new.rendered) {
195198
use diff::Result::{Both, Left, Right};
196199

197200
match change {

lintcheck/src/output.rs

+7-7
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ impl RustcIce {
5151
/// A single warning that clippy issued while checking a `Crate`
5252
#[derive(Debug, PartialEq, Eq, Hash, Serialize, Deserialize)]
5353
pub struct ClippyWarning {
54-
pub lint: String,
54+
pub name: String,
5555
pub diag: Diagnostic,
5656
pub krate: String,
5757
/// The URL that points to the file and line of the lint emission
@@ -60,8 +60,8 @@ pub struct ClippyWarning {
6060

6161
impl ClippyWarning {
6262
pub fn new(mut diag: Diagnostic, base_url: &str, krate: &str) -> Option<Self> {
63-
let lint = diag.code.clone()?.code;
64-
if !(lint.contains("clippy") || diag.message.contains("clippy"))
63+
let name = diag.code.clone()?.code;
64+
if !(name.contains("clippy") || diag.message.contains("clippy"))
6565
|| diag.message.contains("could not read cargo metadata")
6666
{
6767
return None;
@@ -92,7 +92,7 @@ impl ClippyWarning {
9292
};
9393

9494
Some(Self {
95-
lint,
95+
name,
9696
diag,
9797
url,
9898
krate: krate.to_string(),
@@ -108,15 +108,15 @@ impl ClippyWarning {
108108
let mut file = span.file_name.clone();
109109
let file_with_pos = format!("{file}:{}:{}", span.line_start, span.line_end);
110110
match format {
111-
OutputFormat::Text => format!("{file_with_pos} {} \"{}\"\n", self.lint, self.diag.message),
111+
OutputFormat::Text => format!("{file_with_pos} {} \"{}\"\n", self.name, self.diag.message),
112112
OutputFormat::Markdown => {
113113
if file.starts_with("target") {
114114
file.insert_str(0, "../");
115115
}
116116

117117
let mut output = String::from("| ");
118118
write!(output, "[`{file_with_pos}`]({file}#L{})", span.line_start).unwrap();
119-
write!(output, r#" | `{:<50}` | "{}" |"#, self.lint, self.diag.message).unwrap();
119+
write!(output, r#" | `{:<50}` | "{}" |"#, self.name, self.diag.message).unwrap();
120120
output.push('\n');
121121
output
122122
},
@@ -164,7 +164,7 @@ fn gather_stats(warnings: &[ClippyWarning]) -> (String, HashMap<&String, usize>)
164164
let mut counter: HashMap<&String, usize> = HashMap::new();
165165
warnings
166166
.iter()
167-
.for_each(|wrn| *counter.entry(&wrn.lint).or_insert(0) += 1);
167+
.for_each(|wrn| *counter.entry(&wrn.name).or_insert(0) += 1);
168168

169169
// collect into a tupled list for sorting
170170
let mut stats: Vec<(&&String, &usize)> = counter.iter().collect();

0 commit comments

Comments
 (0)