Skip to content

Commit 683ae20

Browse files
committed
switch to secondary annotation
1 parent ab685c1 commit 683ae20

File tree

26 files changed

+92
-231
lines changed

26 files changed

+92
-231
lines changed

crates/ruff_db/src/diagnostic/mod.rs

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -497,16 +497,6 @@ impl Diagnostic {
497497
other.expect_range().start(),
498498
))
499499
}
500-
501-
/// Sort this diagnostic's sub-diagnostics by descending severity.
502-
///
503-
/// This is particularly helpful to move `help`-level sub-diagnostics to the end, after any
504-
/// longer, `info`-level diagnostics.
505-
pub fn sort_sub_diagnostics(&mut self) {
506-
Arc::make_mut(&mut self.inner)
507-
.subs
508-
.sort_by_key(|sub| std::cmp::Reverse(sub.inner.severity));
509-
}
510500
}
511501

512502
#[derive(Debug, Clone, Eq, PartialEq, Hash, get_size2::GetSize)]

crates/ruff_linter/src/checkers/ast/mod.rs

Lines changed: 6 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,7 @@ use itertools::Itertools;
2828
use log::debug;
2929
use rustc_hash::{FxHashMap, FxHashSet};
3030

31-
use ruff_db::diagnostic::{
32-
Annotation, Diagnostic, IntoDiagnosticMessage, Span, SubDiagnostic, SubDiagnosticSeverity,
33-
};
31+
use ruff_db::diagnostic::{Annotation, Diagnostic, IntoDiagnosticMessage, Span};
3432
use ruff_diagnostics::{Applicability, Fix, IsolationLevel};
3533
use ruff_notebook::{CellOffsets, NotebookIndex};
3634
use ruff_python_ast::helpers::{collect_import_from_member, is_docstring_stmt, to_module_path};
@@ -3308,20 +3306,15 @@ impl DiagnosticGuard<'_, '_> {
33083306
}
33093307
}
33103308

3311-
/// Add an "info" sub-diagnostic with the given message and range.
3312-
///
3313-
/// Note that this shadows `Diagnostic::info` from the `Deref` implementation because we'll
3314-
/// usually want to attach a range here.
3315-
pub(crate) fn info<'a>(
3309+
/// Add a secondary annotation with the given message and range.
3310+
pub(crate) fn secondary_annotation<'a>(
33163311
&mut self,
33173312
message: impl IntoDiagnosticMessage + 'a,
33183313
range: impl Ranged,
33193314
) {
3320-
let mut sub = SubDiagnostic::new(SubDiagnosticSeverity::Info, message);
33213315
let span = Span::from(self.context.source_file.clone()).with_range(range.range());
3322-
let ann = Annotation::primary(span);
3323-
sub.annotate(ann);
3324-
self.diagnostic.as_mut().unwrap().sub(sub);
3316+
let ann = Annotation::secondary(span).message(message);
3317+
self.diagnostic.as_mut().unwrap().annotate(ann);
33253318
}
33263319
}
33273320

@@ -3349,8 +3342,7 @@ impl Drop for DiagnosticGuard<'_, '_> {
33493342
return;
33503343
}
33513344

3352-
if let Some(mut diagnostic) = self.diagnostic.take() {
3353-
diagnostic.sort_sub_diagnostics();
3345+
if let Some(diagnostic) = self.diagnostic.take() {
33543346
self.context.diagnostics.borrow_mut().push(diagnostic);
33553347
}
33563348
}

crates/ruff_linter/src/rules/pyflakes/rules/redefined_while_unused.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -192,7 +192,7 @@ pub(crate) fn redefined_while_unused(checker: &Checker, scope_id: ScopeId, scope
192192
binding.range(),
193193
);
194194

195-
diagnostic.info(
195+
diagnostic.secondary_annotation(
196196
format_args!("previous definition of `{name}` here"),
197197
shadowed,
198198
);

crates/ruff_linter/src/rules/pyflakes/snapshots/ruff_linter__rules__pyflakes__tests__F811_F811_0.py.snap

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,11 @@ F811 Redefinition of unused `bar` from line 6
88
| ^^^
99
11 | pass
1010
|
11-
info: previous definition of `bar` here
12-
--> F811_0.py:6:5
13-
|
14-
5 | @foo
15-
6 | def bar():
16-
| ^^^
17-
7 | pass
18-
|
11+
::: F811_0.py:6:5
12+
|
13+
5 | @foo
14+
6 | def bar():
15+
| --- previous definition of `bar` here
16+
7 | pass
17+
|
1918
help: Remove definition: `bar`

crates/ruff_linter/src/rules/pyflakes/snapshots/ruff_linter__rules__pyflakes__tests__F811_F811_1.py.snap

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,11 @@
22
source: crates/ruff_linter/src/rules/pyflakes/mod.rs
33
---
44
F811 Redefinition of unused `FU` from line 1
5-
--> F811_1.py:1:25
6-
|
7-
1 | import fu as FU, bar as FU
8-
| ^^
9-
|
10-
info: previous definition of `FU` here
115
--> F811_1.py:1:14
126
|
137
1 | import fu as FU, bar as FU
14-
| ^^
8+
| -- ^^
9+
| |
10+
| previous definition of `FU` here
1511
|
1612
help: Remove definition: `FU`

crates/ruff_linter/src/rules/pyflakes/snapshots/ruff_linter__rules__pyflakes__tests__F811_F811_12.py.snap

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,21 +2,16 @@
22
source: crates/ruff_linter/src/rules/pyflakes/mod.rs
33
---
44
F811 Redefinition of unused `mixer` from line 2
5-
--> F811_12.py:6:20
6-
|
7-
4 | pass
8-
5 | else:
9-
6 | from bb import mixer
10-
| ^^^^^
11-
7 | mixer(123)
12-
|
13-
info: previous definition of `mixer` here
145
--> F811_12.py:2:20
156
|
167
1 | try:
178
2 | from aa import mixer
18-
| ^^^^^
9+
| ----- previous definition of `mixer` here
1910
3 | except ImportError:
2011
4 | pass
12+
5 | else:
13+
6 | from bb import mixer
14+
| ^^^^^
15+
7 | mixer(123)
2116
|
2217
help: Remove definition: `mixer`

crates/ruff_linter/src/rules/pyflakes/snapshots/ruff_linter__rules__pyflakes__tests__F811_F811_15.py.snap

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,9 @@ F811 Redefinition of unused `fu` from line 1
88
| ^^
99
5 | pass
1010
|
11-
info: previous definition of `fu` here
12-
--> F811_15.py:1:8
11+
::: F811_15.py:1:8
1312
|
1413
1 | import fu
15-
| ^^
14+
| -- previous definition of `fu` here
1615
|
1716
help: Remove definition: `fu`

crates/ruff_linter/src/rules/pyflakes/snapshots/ruff_linter__rules__pyflakes__tests__F811_F811_16.py.snap

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,11 @@ F811 Redefinition of unused `fu` from line 3
1010
| ^^
1111
9 | pass
1212
|
13-
info: previous definition of `fu` here
14-
--> F811_16.py:3:8
13+
::: F811_16.py:3:8
1514
|
1615
1 | """Test that shadowing a global with a nested function generates a warning."""
1716
2 |
1817
3 | import fu
19-
| ^^
18+
| -- previous definition of `fu` here
2019
|
2120
help: Remove definition: `fu`

crates/ruff_linter/src/rules/pyflakes/snapshots/ruff_linter__rules__pyflakes__tests__F811_F811_17.py.snap

Lines changed: 7 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,11 @@ F811 [*] Redefinition of unused `fu` from line 2
1010
7 |
1111
8 | def baz():
1212
|
13-
info: previous definition of `fu` here
14-
--> F811_17.py:2:8
13+
::: F811_17.py:2:8
1514
|
1615
1 | """Test that shadowing a global name with a nested function generates a warning."""
1716
2 | import fu
18-
| ^^
17+
| -- previous definition of `fu` here
1918
|
2019
help: Remove definition: `fu`
2120

@@ -29,20 +28,15 @@ help: Remove definition: `fu`
2928
9 8 | def fu():
3029

3130
F811 Redefinition of unused `fu` from line 6
32-
--> F811_17.py:9:13
31+
--> F811_17.py:6:12
3332
|
33+
5 | def bar():
34+
6 | import fu
35+
| -- previous definition of `fu` here
36+
7 |
3437
8 | def baz():
3538
9 | def fu():
3639
| ^^
3740
10 | pass
3841
|
39-
info: previous definition of `fu` here
40-
--> F811_17.py:6:12
41-
|
42-
5 | def bar():
43-
6 | import fu
44-
| ^^
45-
7 |
46-
8 | def baz():
47-
|
4842
help: Remove definition: `fu`

crates/ruff_linter/src/rules/pyflakes/snapshots/ruff_linter__rules__pyflakes__tests__F811_F811_2.py.snap

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,11 @@
22
source: crates/ruff_linter/src/rules/pyflakes/mod.rs
33
---
44
F811 Redefinition of unused `FU` from line 1
5-
--> F811_2.py:1:34
6-
|
7-
1 | from moo import fu as FU, bar as FU
8-
| ^^
9-
|
10-
info: previous definition of `FU` here
115
--> F811_2.py:1:23
126
|
137
1 | from moo import fu as FU, bar as FU
14-
| ^^
8+
| -- ^^
9+
| |
10+
| previous definition of `FU` here
1511
|
1612
help: Remove definition: `FU`

0 commit comments

Comments
 (0)