Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 11 additions & 1 deletion crates/ruff_linter/src/checkers/ast/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3365,6 +3365,7 @@ impl<'a> LintContext<'a> {
) -> DiagnosticGuard<'chk, 'a> {
DiagnosticGuard {
context: self,
fix_title: kind.fix_title(),
diagnostic: Some(kind.into_diagnostic(range, &self.source_file)),
rule: T::rule(),
}
Expand All @@ -3384,6 +3385,7 @@ impl<'a> LintContext<'a> {
if self.is_rule_enabled(rule) {
Some(DiagnosticGuard {
context: self,
fix_title: kind.fix_title(),
diagnostic: Some(kind.into_diagnostic(range, &self.source_file)),
rule,
})
Expand Down Expand Up @@ -3447,6 +3449,11 @@ pub(crate) struct DiagnosticGuard<'a, 'b> {
///
/// This is always `Some` until the `Drop` (or `defuse`) call.
diagnostic: Option<Diagnostic>,
/// The optional help message to display.
///
/// This is stored here so that it can be added to the `Diagnostic` in the `Drop` impl and
/// render just above the diff, even if other sub-diagnostics have been added.
fix_title: Option<String>,
rule: Rule,
}

Expand Down Expand Up @@ -3592,7 +3599,10 @@ impl Drop for DiagnosticGuard<'_, '_> {
return;
}

if let Some(diagnostic) = self.diagnostic.take() {
if let Some(mut diagnostic) = self.diagnostic.take() {
if let Some(fix_title) = self.fix_title.take() {
diagnostic.help(fix_title);
}
self.context.diagnostics.borrow_mut().push(diagnostic);
}
}
Expand Down
18 changes: 5 additions & 13 deletions crates/ruff_linter/src/message/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -75,10 +75,8 @@ pub fn create_panic_diagnostic(error: &PanicError, path: Option<&Path>) -> Diagn
diagnostic
}

#[expect(clippy::too_many_arguments)]
pub fn create_lint_diagnostic<B, S>(
pub fn create_lint_diagnostic<B>(
body: B,
suggestion: Option<S>,
range: TextRange,
fix: Option<Fix>,
parent: Option<TextSize>,
Expand All @@ -88,7 +86,6 @@ pub fn create_lint_diagnostic<B, S>(
) -> Diagnostic
where
B: Display,
S: Display,
{
let mut diagnostic = Diagnostic::new(
DiagnosticId::Lint(LintName::of(rule.into())),
Expand All @@ -108,10 +105,6 @@ where
}
diagnostic.annotate(annotation);

if let Some(suggestion) = suggestion {
diagnostic.help(suggestion);
}

if let Some(fix) = fix {
diagnostic.set_fix(fix);
}
Expand Down Expand Up @@ -278,9 +271,8 @@ def fibonacci(n):
let fib_source = SourceFileBuilder::new("fib.py", fib).finish();

let unused_import_start = TextSize::from(7);
let unused_import = create_lint_diagnostic(
let mut unused_import = create_lint_diagnostic(
"`os` imported but unused",
Some("Remove unused import: `os`"),
TextRange::new(unused_import_start, TextSize::from(9)),
Some(Fix::unsafe_edit(Edit::range_deletion(TextRange::new(
TextSize::from(0),
Expand All @@ -291,11 +283,11 @@ def fibonacci(n):
Some(unused_import_start),
Rule::UnusedImport,
);
unused_import.help("Remove unused import: `os`");

let unused_variable_start = TextSize::from(94);
let unused_variable = create_lint_diagnostic(
let mut unused_variable = create_lint_diagnostic(
"Local variable `x` is assigned to but never used",
Some("Remove assignment to unused variable `x`"),
TextRange::new(unused_variable_start, TextSize::from(95)),
Some(Fix::unsafe_edit(Edit::deletion(
TextSize::from(94),
Expand All @@ -306,13 +298,13 @@ def fibonacci(n):
Some(unused_variable_start),
Rule::UnusedVariable,
);
unused_variable.help("Remove assignment to unused variable `x`");

let file_2 = r"if a == 1: pass";

let undefined_name_start = TextSize::from(3);
let undefined_name = create_lint_diagnostic(
"Undefined name `a`",
Option::<&'static str>::None,
TextRange::new(undefined_name_start, TextSize::from(4)),
None,
None,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ ISC004 [*] Unparenthesized implicit string concatenation in collection
| |______________________________________________________________^
6 | )
|
help: Wrap implicitly concatenated strings in parentheses
help: Did you forget a comma?
help: Wrap implicitly concatenated strings in parentheses
1 | facts = (
2 | "Lobsters have blue blood.",
3 | "The liver is the only human organ that can fully regenerate itself.",
Expand All @@ -35,8 +35,8 @@ ISC004 [*] Unparenthesized implicit string concatenation in collection
| |______________________________________________________________^
13 | ]
|
help: Wrap implicitly concatenated strings in parentheses
help: Did you forget a comma?
help: Wrap implicitly concatenated strings in parentheses
8 | facts = [
9 | "Lobsters have blue blood.",
10 | "The liver is the only human organ that can fully regenerate itself.",
Expand All @@ -59,8 +59,8 @@ ISC004 [*] Unparenthesized implicit string concatenation in collection
| |______________________________________________________________^
20 | }
|
help: Wrap implicitly concatenated strings in parentheses
help: Did you forget a comma?
help: Wrap implicitly concatenated strings in parentheses
15 | facts = {
16 | "Lobsters have blue blood.",
17 | "The liver is the only human organ that can fully regenerate itself.",
Expand All @@ -83,8 +83,8 @@ ISC004 [*] Unparenthesized implicit string concatenation in collection
| |_________________________^
33 | )
|
help: Wrap implicitly concatenated strings in parentheses
help: Did you forget a comma?
help: Wrap implicitly concatenated strings in parentheses
27 | }
28 |
29 | facts = (
Expand All @@ -108,8 +108,8 @@ ISC004 [*] Unparenthesized implicit string concatenation in collection
| |_________________________^
39 | ]
|
help: Wrap implicitly concatenated strings in parentheses
help: Did you forget a comma?
help: Wrap implicitly concatenated strings in parentheses
33 | )
34 |
35 | facts = [
Expand All @@ -133,8 +133,8 @@ ISC004 [*] Unparenthesized implicit string concatenation in collection
| |_________________________^
45 | }
|
help: Wrap implicitly concatenated strings in parentheses
help: Did you forget a comma?
help: Wrap implicitly concatenated strings in parentheses
39 | ]
40 |
41 | facts = {
Expand Down
Loading
Loading