Skip to content

Commit 74a4e9a

Browse files
authored
Combine lint and syntax error handling (#18471)
## Summary This is a spin-off from #18447 (comment) to avoid using `Message::noqa_code` to differentiate between lints and syntax errors. I went through all of the calls on `main` and on the branch from #18447, and the instance in `ruff_server` noted in the linked comment was actually the primary place where this was being done. Other calls to `noqa_code` are typically some variation of `message.noqa_code().map_or(String::new, format!(...))`, with the major exception of the gitlab output format: https://github.com/astral-sh/ruff/blob/a120610b5b01a9e7bb91740a23f6c2b5bbcd4b5f/crates/ruff_linter/src/message/gitlab.rs#L93-L105 which obviously assumes that `None` means syntax error. A simple fix here would be to use `message.name()` for `check_name` instead of the noqa code, but I'm not sure how breaking that would be. This could just be: ```rust let description = message.body(); let description = description.strip_prefix("SyntaxError: ").unwrap_or(description).to_string(); let check_name = message.name(); ``` In that case. This sounds reasonable based on the [Code Quality report format](https://docs.gitlab.com/ci/testing/code_quality/#code-quality-report-format) docs: > | Name | Type | Description| > |-----|-----|----| > |`check_name` | String | A unique name representing the check, or rule, associated with this violation. | ## Test Plan Existing tests
1 parent 8485dbb commit 74a4e9a

File tree

2 files changed

+46
-98
lines changed

2 files changed

+46
-98
lines changed

crates/ruff_server/src/lint.rs

Lines changed: 29 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ use crate::{
1212
use ruff_diagnostics::{Applicability, Edit, Fix};
1313
use ruff_linter::{
1414
Locator,
15-
codes::Rule,
1615
directives::{Flags, extract_directives},
1716
generate_noqa_edits,
1817
linter::check_path,
@@ -166,26 +165,17 @@ pub(crate) fn check(
166165
messages
167166
.into_iter()
168167
.zip(noqa_edits)
169-
.filter_map(|(message, noqa_edit)| match message.to_rule() {
170-
Some(rule) => Some(to_lsp_diagnostic(
171-
rule,
172-
&message,
173-
noqa_edit,
174-
&source_kind,
175-
locator.to_index(),
176-
encoding,
177-
)),
178-
None => {
179-
if show_syntax_errors {
180-
Some(syntax_error_to_lsp_diagnostic(
181-
&message,
182-
&source_kind,
183-
locator.to_index(),
184-
encoding,
185-
))
186-
} else {
187-
None
188-
}
168+
.filter_map(|(message, noqa_edit)| {
169+
if message.is_syntax_error() && !show_syntax_errors {
170+
None
171+
} else {
172+
Some(to_lsp_diagnostic(
173+
&message,
174+
noqa_edit,
175+
&source_kind,
176+
locator.to_index(),
177+
encoding,
178+
))
189179
}
190180
});
191181

@@ -241,7 +231,6 @@ pub(crate) fn fixes_for_diagnostics(
241231
/// Generates an LSP diagnostic with an associated cell index for the diagnostic to go in.
242232
/// If the source kind is a text document, the cell index will always be `0`.
243233
fn to_lsp_diagnostic(
244-
rule: Rule,
245234
diagnostic: &Message,
246235
noqa_edit: Option<Edit>,
247236
source_kind: &SourceKind,
@@ -253,11 +242,13 @@ fn to_lsp_diagnostic(
253242
let body = diagnostic.body().to_string();
254243
let fix = diagnostic.fix();
255244
let suggestion = diagnostic.suggestion();
245+
let code = diagnostic.to_noqa_code();
256246

257247
let fix = fix.and_then(|fix| fix.applies(Applicability::Unsafe).then_some(fix));
258248

259249
let data = (fix.is_some() || noqa_edit.is_some())
260250
.then(|| {
251+
let code = code?.to_string();
261252
let edits = fix
262253
.into_iter()
263254
.flat_map(Fix::edits)
@@ -274,14 +265,12 @@ fn to_lsp_diagnostic(
274265
title: suggestion.unwrap_or(name).to_string(),
275266
noqa_edit,
276267
edits,
277-
code: rule.noqa_code().to_string(),
268+
code,
278269
})
279270
.ok()
280271
})
281272
.flatten();
282273

283-
let code = rule.noqa_code().to_string();
284-
285274
let range: lsp_types::Range;
286275
let cell: usize;
287276

@@ -297,14 +286,25 @@ fn to_lsp_diagnostic(
297286
range = diagnostic_range.to_range(source_kind.source_code(), index, encoding);
298287
}
299288

289+
let (severity, tags, code) = if let Some(code) = code {
290+
let code = code.to_string();
291+
(
292+
Some(severity(&code)),
293+
tags(&code),
294+
Some(lsp_types::NumberOrString::String(code)),
295+
)
296+
} else {
297+
(None, None, None)
298+
};
299+
300300
(
301301
cell,
302302
lsp_types::Diagnostic {
303303
range,
304-
severity: Some(severity(&code)),
305-
tags: tags(&code),
306-
code: Some(lsp_types::NumberOrString::String(code)),
307-
code_description: rule.url().and_then(|url| {
304+
severity,
305+
tags,
306+
code,
307+
code_description: diagnostic.to_url().and_then(|url| {
308308
Some(lsp_types::CodeDescription {
309309
href: lsp_types::Url::parse(&url).ok()?,
310310
})
@@ -317,45 +317,6 @@ fn to_lsp_diagnostic(
317317
)
318318
}
319319

320-
fn syntax_error_to_lsp_diagnostic(
321-
syntax_error: &Message,
322-
source_kind: &SourceKind,
323-
index: &LineIndex,
324-
encoding: PositionEncoding,
325-
) -> (usize, lsp_types::Diagnostic) {
326-
let range: lsp_types::Range;
327-
let cell: usize;
328-
329-
if let Some(notebook_index) = source_kind.as_ipy_notebook().map(Notebook::index) {
330-
NotebookRange { cell, range } = syntax_error.range().to_notebook_range(
331-
source_kind.source_code(),
332-
index,
333-
notebook_index,
334-
encoding,
335-
);
336-
} else {
337-
cell = usize::default();
338-
range = syntax_error
339-
.range()
340-
.to_range(source_kind.source_code(), index, encoding);
341-
}
342-
343-
(
344-
cell,
345-
lsp_types::Diagnostic {
346-
range,
347-
severity: Some(lsp_types::DiagnosticSeverity::ERROR),
348-
tags: None,
349-
code: None,
350-
code_description: None,
351-
source: Some(DIAGNOSTIC_NAME.into()),
352-
message: syntax_error.body().to_string(),
353-
related_information: None,
354-
data: None,
355-
},
356-
)
357-
}
358-
359320
fn diagnostic_edit_range(
360321
range: TextRange,
361322
source_kind: &SourceKind,

crates/ruff_wasm/src/lib.rs

Lines changed: 17 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -207,36 +207,23 @@ impl Workspace {
207207

208208
let messages: Vec<ExpandedMessage> = messages
209209
.into_iter()
210-
.map(|msg| {
211-
let message = msg.body().to_string();
212-
let range = msg.range();
213-
match msg.to_noqa_code() {
214-
Some(code) => ExpandedMessage {
215-
code: Some(code.to_string()),
216-
message,
217-
start_location: source_code.line_column(range.start()).into(),
218-
end_location: source_code.line_column(range.end()).into(),
219-
fix: msg.fix().map(|fix| ExpandedFix {
220-
message: msg.suggestion().map(ToString::to_string),
221-
edits: fix
222-
.edits()
223-
.iter()
224-
.map(|edit| ExpandedEdit {
225-
location: source_code.line_column(edit.start()).into(),
226-
end_location: source_code.line_column(edit.end()).into(),
227-
content: edit.content().map(ToString::to_string),
228-
})
229-
.collect(),
230-
}),
231-
},
232-
None => ExpandedMessage {
233-
code: None,
234-
message,
235-
start_location: source_code.line_column(range.start()).into(),
236-
end_location: source_code.line_column(range.end()).into(),
237-
fix: None,
238-
},
239-
}
210+
.map(|msg| ExpandedMessage {
211+
code: msg.to_noqa_code().map(|code| code.to_string()),
212+
message: msg.body().to_string(),
213+
start_location: source_code.line_column(msg.start()).into(),
214+
end_location: source_code.line_column(msg.end()).into(),
215+
fix: msg.fix().map(|fix| ExpandedFix {
216+
message: msg.suggestion().map(ToString::to_string),
217+
edits: fix
218+
.edits()
219+
.iter()
220+
.map(|edit| ExpandedEdit {
221+
location: source_code.line_column(edit.start()).into(),
222+
end_location: source_code.line_column(edit.end()).into(),
223+
content: edit.content().map(ToString::to_string),
224+
})
225+
.collect(),
226+
}),
240227
})
241228
.collect();
242229

0 commit comments

Comments
 (0)