Skip to content

Commit 7bc7fe5

Browse files
committed
clippy_dev: print parse errors with location info.
1 parent 107ab93 commit 7bc7fe5

File tree

5 files changed

+434
-162
lines changed

5 files changed

+434
-162
lines changed

clippy_dev/src/deprecate_lint.rs

Lines changed: 18 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use crate::parse::{ActiveLint, DeprecatedLint, Lint, ParsedData};
1+
use crate::parse::{ActiveLint, DeprecatedLint, LintKind, ParsedData};
22
use crate::update_lints::generate_lint_files;
33
use crate::utils::{FileUpdater, UpdateMode, UpdateStatus, Version, delete_dir_if_exists, delete_file_if_exists};
44
use core::mem;
@@ -24,9 +24,9 @@ pub fn deprecate(clippy_version: Version, name: &str, reason: &str) {
2424
eprintln!("error: failed to find lint `{name}`");
2525
return;
2626
};
27-
let Lint::Active(lint) = mem::replace(
28-
entry,
29-
Lint::Deprecated(DeprecatedLint {
27+
let LintKind::Active(lint) = mem::replace(
28+
&mut entry.kind,
29+
LintKind::Deprecated(DeprecatedLint {
3030
reason: reason.into(),
3131
version: clippy_version.rust_display().to_string(),
3232
}),
@@ -83,14 +83,10 @@ fn remove_lint_declaration(name: &str, lint: &ActiveLint, data: &ParsedData) {
8383
}
8484
}
8585

86-
let lint_file = &data.source_map.files[lint.span.file];
86+
let lint_file = &data.source_map.files[lint.decl_span.file];
8787
if data.lints.values().any(|l| {
88-
if let Lint::Active(l) = l {
89-
let other_file = &data.source_map.files[l.span.file];
90-
other_file.krate == lint_file.krate && other_file.module.starts_with(&lint_file.module)
91-
} else {
92-
false
93-
}
88+
let other_file = &data.source_map.files[l.name_span.file];
89+
other_file.krate == lint_file.krate && other_file.module.starts_with(&lint_file.module)
9490
}) {
9591
// Try to delete a sub-module that matches the lint's name
9692
let removed_mod = if lint_file.path.file_name().map(OsStr::as_encoded_bytes) == Some(b"mod.rs") {
@@ -107,23 +103,28 @@ fn remove_lint_declaration(name: &str, lint: &ActiveLint, data: &ParsedData) {
107103
&& let mod_decl = format!("\nmod {name};")
108104
&& let Some(mod_start) = src.find(&mod_decl)
109105
{
110-
if mod_start < lint.span.start as usize {
106+
if mod_start < lint.decl_span.start as usize {
111107
(
112108
mod_start,
113109
mod_start + mod_decl.len(),
114-
lint.span.start as usize,
115-
lint.span.end as usize,
110+
lint.decl_span.start as usize,
111+
lint.decl_span.end as usize,
116112
)
117113
} else {
118114
(
119-
lint.span.start as usize,
120-
lint.span.end as usize,
115+
lint.decl_span.start as usize,
116+
lint.decl_span.end as usize,
121117
mod_start,
122118
mod_start + mod_decl.len(),
123119
)
124120
}
125121
} else {
126-
(lint.span.start as usize, lint.span.end as usize, src.len(), src.len())
122+
(
123+
lint.decl_span.start as usize,
124+
lint.decl_span.end as usize,
125+
src.len(),
126+
src.len(),
127+
)
127128
};
128129
dst.push_str(&src[..a]);
129130
dst.push_str(&src[b..c]);

clippy_dev/src/new_lint.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use crate::parse::{RustSearcher, Token};
1+
use crate::parse::{Capture, RustSearcher, Token};
22
use crate::utils::Version;
33
use clap::ValueEnum;
44
use indoc::{formatdoc, writedoc};
@@ -523,6 +523,7 @@ fn parse_mod_file(path: &Path, contents: &str) -> (&'static str, usize) {
523523
let mut context = None;
524524
let mut decl_end = None;
525525
let mut searcher = RustSearcher::new(contents);
526+
let mut captures = [Capture::EMPTY];
526527
while let Some(name) = searcher.find_capture_token(CaptureIdent) {
527528
match name {
528529
"declare_clippy_lint" => {
@@ -531,9 +532,8 @@ fn parse_mod_file(path: &Path, contents: &str) -> (&'static str, usize) {
531532
}
532533
},
533534
"impl" => {
534-
let mut capture = "";
535-
if searcher.match_tokens(&[Lt, Lifetime, Gt, CaptureIdent], &mut [&mut capture]) {
536-
match capture {
535+
if searcher.match_tokens(&[Lt, Lifetime, Gt, CaptureIdent], &mut captures) {
536+
match searcher.get_capture(captures[0]) {
537537
"LateLintPass" => context = Some("LateContext"),
538538
"EarlyLintPass" => context = Some("EarlyContext"),
539539
_ => {},

0 commit comments

Comments
 (0)