-
Notifications
You must be signed in to change notification settings - Fork 2k
Expand file tree
/
Copy pathsuppression.rs
More file actions
678 lines (596 loc) · 22.3 KB
/
suppression.rs
File metadata and controls
678 lines (596 loc) · 22.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
mod add_ignore;
mod parser;
mod unused;
use smallvec::SmallVec;
use std::fmt;
use ruff_db::diagnostic::{
Annotation, Diagnostic, DiagnosticId, IntoDiagnosticMessage, LintName, Severity, Span,
};
use ruff_db::{files::File, parsed::parsed_module, source::source_text};
use ruff_python_ast::token::TokenKind;
use ruff_text_size::{Ranged, TextLen, TextRange, TextSize};
use crate::diagnostic::DiagnosticGuard;
use crate::lint::{GetLintError, Level, LintMetadata, LintRegistry, LintStatus};
pub use crate::suppression::add_ignore::{SuppressFix, suppress_all, suppress_single};
use crate::suppression::parser::{
ParseError, ParseErrorKind, SuppressionComment, SuppressionParser,
};
use crate::suppression::unused::check_unused_suppressions;
use crate::types::TypeCheckDiagnostics;
use crate::{Db, declare_lint, lint::LintId};
declare_lint! {
/// ## What it does
/// Checks for `ty: ignore` directives that are no longer applicable.
///
/// ## Why is this bad?
/// A `ty: ignore` directive that no longer matches any diagnostic violations is likely
/// included by mistake, and should be removed to avoid confusion.
///
/// ## Examples
/// ```py
/// a = 20 / 2 # ty: ignore[division-by-zero]
/// ```
///
/// Use instead:
///
/// ```py
/// a = 20 / 2
/// ```
///
/// ## Options
/// Set [`analysis.respect-type-ignore-comments`](https://docs.astral.sh/ty/reference/configuration/#respect-type-ignore-comments)
/// to `false` to prevent this rule from reporting unused `type: ignore` comments.
pub static UNUSED_IGNORE_COMMENT = {
summary: "detects unused `ty: ignore` comments",
status: LintStatus::stable("0.0.1-alpha.1"),
default_level: Level::Warn,
}
}
declare_lint! {
/// ## What it does
/// Checks for `type: ignore` directives that are no longer applicable.
///
/// ## Why is this bad?
/// A `type: ignore` directive that no longer matches any diagnostic violations is likely
/// included by mistake, and should be removed to avoid confusion.
///
/// ## Examples
/// ```py
/// a = 20 / 2 # type: ignore
/// ```
///
/// Use instead:
///
/// ```py
/// a = 20 / 2
/// ```
///
/// ## Options
///
/// This rule is skipped if [`analysis.respect-type-ignore-comments`](https://docs.astral.sh/ty/reference/configuration/#respect-type-ignore-comments)
/// to `false`.
pub(crate) static UNUSED_TYPE_IGNORE_COMMENT = {
summary: "detects unused `type: ignore` comments",
status: LintStatus::stable("0.0.14"),
default_level: Level::Warn,
}
}
declare_lint! {
/// ## What it does
/// Checks for `ty: ignore[code]` or `type: ignore[ty:code]` comments where `code` isn't a known lint rule.
///
/// ## Why is this bad?
/// A `ty: ignore[code]` or a `type:ignore[ty:code] directive with a `code` that doesn't match
/// any known rule will not suppress any type errors, and is probably a mistake.
///
/// ## Examples
/// ```py
/// a = 20 / 0 # ty: ignore[division-by-zer]
/// ```
///
/// Use instead:
///
/// ```py
/// a = 20 / 0 # ty: ignore[division-by-zero]
/// ```
pub(crate) static IGNORE_COMMENT_UNKNOWN_RULE = {
summary: "detects `ty: ignore` comments that reference unknown rules",
status: LintStatus::stable("0.0.1-alpha.1"),
default_level: Level::Warn,
}
}
declare_lint! {
/// ## What it does
/// Checks for `type: ignore` and `ty: ignore` comments that are syntactically incorrect.
///
/// ## Why is this bad?
/// A syntactically incorrect ignore comment is probably a mistake and is useless.
///
/// ## Examples
/// ```py
/// a = 20 / 0 # type: ignoree
/// ```
///
/// Use instead:
///
/// ```py
/// a = 20 / 0 # type: ignore
/// ```
pub(crate) static INVALID_IGNORE_COMMENT = {
summary: "detects ignore comments that use invalid syntax",
status: LintStatus::stable("0.0.1-alpha.1"),
default_level: Level::Warn,
}
}
pub fn is_unused_ignore_comment_lint(name: LintName) -> bool {
name == UNUSED_IGNORE_COMMENT.name() || name == UNUSED_TYPE_IGNORE_COMMENT.name()
}
#[salsa::tracked(returns(ref), heap_size=ruff_memory_usage::heap_size)]
pub(crate) fn suppressions(db: &dyn Db, file: File) -> Suppressions {
let parsed = parsed_module(db, file).load(db);
let source = source_text(db, file);
let respect_type_ignore = db.analysis_settings(file).respect_type_ignore_comments;
let mut builder = SuppressionsBuilder::new(&source, db.lint_registry());
let mut line_start = TextSize::default();
for token in parsed.tokens() {
if !token.kind().is_trivia() {
builder.set_seen_non_trivia_token();
}
match token.kind() {
TokenKind::Comment => {
let parser = SuppressionParser::new(&source, token.range());
for comment in parser {
match comment {
Ok(comment) => {
if comment.kind().is_type_ignore() && !respect_type_ignore {
continue;
}
builder.add_comment(comment, TextRange::new(line_start, token.end()));
}
Err(error) => match error.kind {
ParseErrorKind::NotASuppression
| ParseErrorKind::CommentWithoutHash => {
// Skip non suppression comments and comments that miss a hash (shouldn't ever happen)
}
ParseErrorKind::NoWhitespaceAfterIgnore(kind)
| ParseErrorKind::CodesMissingComma(kind)
| ParseErrorKind::InvalidCode(kind)
| ParseErrorKind::CodesMissingClosingBracket(kind) => {
if kind.is_type_ignore() && !respect_type_ignore {
continue;
}
builder.add_invalid_comment(kind, error);
}
},
}
}
}
TokenKind::Newline | TokenKind::NonLogicalNewline => {
line_start = token.end();
}
_ => {}
}
}
builder.finish()
}
pub(crate) fn check_suppressions(
db: &dyn Db,
file: File,
diagnostics: TypeCheckDiagnostics,
) -> Vec<Diagnostic> {
let mut context = CheckSuppressionsContext::new(db, file, diagnostics);
check_unknown_rule(&mut context);
check_invalid_suppression(&mut context);
check_unused_suppressions(&mut context);
context.diagnostics.into_inner().into_diagnostics()
}
/// Checks for `ty: ignore` and `type: ignore[ty:<code>]` comments that reference unknown rules.
fn check_unknown_rule(context: &mut CheckSuppressionsContext) {
if context.is_lint_disabled(&IGNORE_COMMENT_UNKNOWN_RULE) {
return;
}
for unknown in &context.suppressions.unknown {
if let Some(diag) = context.report_lint(&IGNORE_COMMENT_UNKNOWN_RULE, unknown.range) {
diag.into_diagnostic(&unknown.reason);
}
}
}
fn check_invalid_suppression(context: &mut CheckSuppressionsContext) {
if context.is_lint_disabled(&INVALID_IGNORE_COMMENT) {
return;
}
for invalid in &context.suppressions.invalid {
if let Some(diag) = context.report_lint(&INVALID_IGNORE_COMMENT, invalid.error.range) {
diag.into_diagnostic(format_args!(
"Invalid `{kind}` comment: {reason}",
kind = invalid.kind,
reason = &invalid.error
));
}
}
}
struct CheckSuppressionsContext<'a> {
db: &'a dyn Db,
file: File,
suppressions: &'a Suppressions,
diagnostics: std::cell::RefCell<TypeCheckDiagnostics>,
}
impl<'a> CheckSuppressionsContext<'a> {
fn new(db: &'a dyn Db, file: File, diagnostics: TypeCheckDiagnostics) -> Self {
let suppressions = suppressions(db, file);
Self {
db,
file,
suppressions,
diagnostics: diagnostics.into(),
}
}
fn is_lint_disabled(&self, lint: &'static LintMetadata) -> bool {
!self
.db
.rule_selection(self.file)
.is_enabled(LintId::of(lint))
}
fn is_suppression_used(&self, id: FileSuppressionId) -> bool {
self.diagnostics.borrow().is_used(id)
}
fn report_lint<'ctx>(
&'ctx self,
lint: &'static LintMetadata,
range: TextRange,
) -> Option<SuppressionDiagnosticGuardBuilder<'ctx, 'a>> {
if let Some(suppression) = self.suppressions.find_suppression(range, LintId::of(lint)) {
self.diagnostics.borrow_mut().mark_used(suppression.id());
return None;
}
self.report_unchecked(lint, range)
}
/// Reports a diagnostic without checking if the lint at the given range is suppressed or marking
/// the suppression as used.
fn report_unchecked<'ctx>(
&'ctx self,
lint: &'static LintMetadata,
range: TextRange,
) -> Option<SuppressionDiagnosticGuardBuilder<'ctx, 'a>> {
SuppressionDiagnosticGuardBuilder::new(self, lint, range)
}
}
/// A builder for constructing a diagnostic guard.
///
/// This type exists to separate the phases of "check if a diagnostic should
/// be reported" and "build the actual diagnostic."
pub(crate) struct SuppressionDiagnosticGuardBuilder<'ctx, 'db> {
ctx: &'ctx CheckSuppressionsContext<'db>,
id: DiagnosticId,
range: TextRange,
severity: Severity,
}
impl<'ctx, 'db> SuppressionDiagnosticGuardBuilder<'ctx, 'db> {
fn new(
ctx: &'ctx CheckSuppressionsContext<'db>,
lint: &'static LintMetadata,
range: TextRange,
) -> Option<Self> {
let severity = ctx.db.rule_selection(ctx.file).severity(LintId::of(lint))?;
Some(Self {
ctx,
id: DiagnosticId::Lint(lint.name()),
severity,
range,
})
}
/// Create a new guard.
///
/// This initializes a new diagnostic using the given message along with
/// the ID and severity used to create this builder.
///
/// The diagnostic can be further mutated on the guard via its `DerefMut`
/// impl to `Diagnostic`.
pub(crate) fn into_diagnostic(
self,
message: impl IntoDiagnosticMessage,
) -> DiagnosticGuard<'ctx> {
let mut diag = Diagnostic::new(self.id, self.severity, message);
let primary_span = Span::from(self.ctx.file).with_range(self.range);
diag.annotate(Annotation::primary(primary_span));
DiagnosticGuard::new(self.ctx.file, &self.ctx.diagnostics, diag)
}
}
/// The suppressions of a single file.
#[derive(Debug, Eq, PartialEq, get_size2::GetSize)]
pub(crate) struct Suppressions {
/// Suppressions that apply to the entire file.
///
/// The suppressions are sorted by [`Suppression::comment_range`] and the [`Suppression::suppressed_range`]
/// spans the entire file.
file: SmallVec<[Suppression; 1]>,
/// Suppressions that apply to a specific line (or lines).
///
/// Comments with multiple codes create multiple [`Suppression`]s that all share the same [`Suppression::comment_range`].
///
/// The suppressions are sorted by [`Suppression::range`] (which implies [`Suppression::comment_range`]).
line: Vec<Suppression>,
/// Suppressions with lint codes that are unknown.
unknown: Vec<UnknownSuppression>,
/// Suppressions that are syntactically invalid.
invalid: Vec<InvalidSuppression>,
}
impl Suppressions {
pub(crate) fn find_suppression(&self, range: TextRange, id: LintId) -> Option<&Suppression> {
self.lint_suppressions(range, id).next()
}
/// Returns all suppressions for the given lint
fn lint_suppressions(
&self,
range: TextRange,
id: LintId,
) -> impl Iterator<Item = &Suppression> + '_ {
self.file
.iter()
.chain(self.line_suppressions(range))
.filter(move |suppression| suppression.matches(id))
}
/// Returns the line-level suppressions that apply for `range`.
///
/// A suppression applies for the given range if it contains the range's
/// start or end offset. This means the suppression is on the same line
/// as the diagnostic's start or end.
fn line_suppressions(&self, range: TextRange) -> impl Iterator<Item = &Suppression> + '_ {
// First find the index of the suppression comment that ends right before the range
// starts. This allows us to skip suppressions that are not relevant for the range.
let end_offset = self
.line
.binary_search_by_key(&range.start(), |suppression| {
suppression.suppressed_range.end()
})
.unwrap_or_else(|index| index);
// From here, search the remaining suppression comments for one that
// contains the range's start or end offset. Stop the search
// as soon as the suppression's range and the range no longer overlap.
self.line[end_offset..]
.iter()
// Stop searching if the suppression starts after the range we're looking for.
.take_while(move |suppression| range.end() >= suppression.suppressed_range.start())
.filter(move |suppression| {
// Don't use intersect to avoid that suppressions on inner-expression
// ignore errors for outer expressions
suppression.suppressed_range.contains(range.start())
|| suppression.suppressed_range.contains_inclusive(range.end())
})
}
fn iter(&self) -> SuppressionsIter<'_> {
self.file.iter().chain(&self.line)
}
}
pub(crate) type SuppressionsIter<'a> =
std::iter::Chain<std::slice::Iter<'a, Suppression>, std::slice::Iter<'a, Suppression>>;
impl<'a> IntoIterator for &'a Suppressions {
type Item = &'a Suppression;
type IntoIter = SuppressionsIter<'a>;
fn into_iter(self) -> Self::IntoIter {
self.iter()
}
}
/// A `type: ignore` or `ty: ignore` suppression.
///
/// Suppression comments that suppress multiple codes
/// create multiple suppressions: one for every code.
/// They all share the same `comment_range`.
#[derive(Clone, Debug, Eq, PartialEq, get_size2::GetSize)]
pub(crate) struct Suppression {
target: SuppressionTarget,
kind: SuppressionKind,
/// The range of the code in this suppression.
///
/// This is the same as the `comment_range` for the
/// targets [`SuppressionTarget::All`] and [`SuppressionTarget::Empty`].
range: TextRange,
/// The range of the suppression comment.
///
/// This isn't the range of the entire comment if this is a nested comment:
///
/// ```py
/// a # ty: ignore # fmt: off
/// ^^^^^^^^^^^^^
/// ```
///
/// It doesn't include the range of the nested `# fmt: off` comment.
comment_range: TextRange,
/// The range for which this suppression applies.
/// Most of the time, this is the range of the comment's line.
/// However, there are few cases where the range gets expanded to
/// cover multiple lines:
/// * multiline strings: `expr + """multiline\nstring""" # type: ignore`
/// * line continuations: `expr \ + "test" # type: ignore`
suppressed_range: TextRange,
}
impl Suppression {
fn matches(&self, tested_id: LintId) -> bool {
match self.target {
SuppressionTarget::All => true,
SuppressionTarget::Lint(suppressed_id) => tested_id == suppressed_id,
SuppressionTarget::Empty => false,
}
}
pub(crate) fn id(&self) -> FileSuppressionId {
FileSuppressionId(self.range)
}
}
#[derive(Copy, Clone, Debug, Eq, PartialEq, get_size2::GetSize)]
enum SuppressionKind {
TypeIgnore,
Ty,
}
impl SuppressionKind {
const fn is_type_ignore(self) -> bool {
matches!(self, SuppressionKind::TypeIgnore)
}
fn len_utf8(self) -> usize {
match self {
SuppressionKind::TypeIgnore => "type".len(),
SuppressionKind::Ty => "ty".len(),
}
}
}
impl fmt::Display for SuppressionKind {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
SuppressionKind::TypeIgnore => f.write_str("type: ignore"),
SuppressionKind::Ty => f.write_str("ty: ignore"),
}
}
}
/// Unique ID for a suppression in a file.
///
/// ## Implementation
/// The wrapped `TextRange` is the suppression's range.
/// This is unique enough because it is its exact
/// location in the source.
#[derive(Copy, Clone, Debug, Eq, PartialEq, Hash, get_size2::GetSize)]
pub(crate) struct FileSuppressionId(TextRange);
#[derive(Copy, Clone, Debug, Eq, PartialEq, get_size2::GetSize)]
enum SuppressionTarget {
/// Suppress all lints
All,
/// Suppress the lint with the given id
Lint(LintId),
/// Suppresses no lint, e.g. `ty: ignore[]`
Empty,
}
impl SuppressionTarget {
const fn is_lint(self) -> bool {
matches!(self, SuppressionTarget::Lint(_))
}
}
struct SuppressionsBuilder<'a> {
lint_registry: &'a LintRegistry,
source: &'a str,
/// Ignore comments at the top of the file before any non-trivia code apply to the entire file.
/// This boolean tracks if there has been any non trivia token.
seen_non_trivia_token: bool,
line: Vec<Suppression>,
file: SmallVec<[Suppression; 1]>,
unknown: Vec<UnknownSuppression>,
invalid: Vec<InvalidSuppression>,
}
impl<'a> SuppressionsBuilder<'a> {
fn new(source: &'a str, lint_registry: &'a LintRegistry) -> Self {
Self {
source,
lint_registry,
seen_non_trivia_token: false,
line: Vec::new(),
file: SmallVec::new_const(),
unknown: Vec::new(),
invalid: Vec::new(),
}
}
fn set_seen_non_trivia_token(&mut self) {
self.seen_non_trivia_token = true;
}
fn finish(mut self) -> Suppressions {
self.line.shrink_to_fit();
self.file.shrink_to_fit();
self.unknown.shrink_to_fit();
self.invalid.shrink_to_fit();
Suppressions {
file: self.file,
line: self.line,
unknown: self.unknown,
invalid: self.invalid,
}
}
#[expect(clippy::needless_pass_by_value)]
fn add_comment(&mut self, comment: SuppressionComment, line_range: TextRange) {
// ignore comments at the start of the file apply to the entire range.
// > A # type: ignore comment on a line by itself at the top of a file, before any docstrings,
// > imports, or other executable code, silences all errors in the file.
// > Blank lines and other comments, such as shebang lines and coding cookies,
// > may precede the # type: ignore comment.
// > https://typing.python.org/en/latest/spec/directives.html#type-ignore-comments
let is_file_suppression = !self.seen_non_trivia_token;
let suppressed_range = if is_file_suppression {
TextRange::new(0.into(), self.source.text_len())
} else {
line_range
};
let mut push_ignore_suppression = |suppression: Suppression| {
if is_file_suppression {
self.file.push(suppression);
} else {
self.line.push(suppression);
}
};
match comment.codes() {
// `type: ignore`
None => {
push_ignore_suppression(Suppression {
target: SuppressionTarget::All,
kind: comment.kind(),
comment_range: comment.range(),
range: comment.range(),
suppressed_range,
});
}
// `ty: ignore[]` or `type: ignore[]`
Some([]) => {
push_ignore_suppression(Suppression {
target: SuppressionTarget::Empty,
kind: comment.kind(),
range: comment.range(),
comment_range: comment.range(),
suppressed_range,
});
}
// `ty: ignore[a, b]` or `type: ignore[a, b]`
Some(codes) => {
for &code_range in codes {
let code = &self.source[code_range];
// For `type:ignore`, ignore codes that don't start with `ty:`.
let code = if comment.kind().is_type_ignore() {
if let Some(prefix) = code.strip_prefix("ty:") {
prefix
} else {
continue;
}
} else {
code
};
match self.lint_registry.get(code) {
Ok(lint) => {
push_ignore_suppression(Suppression {
target: SuppressionTarget::Lint(lint),
kind: comment.kind(),
range: code_range,
comment_range: comment.range(),
suppressed_range,
});
}
Err(error) => self.unknown.push(UnknownSuppression {
range: code_range,
comment_range: comment.range(),
reason: error,
}),
}
}
}
}
}
fn add_invalid_comment(&mut self, kind: SuppressionKind, error: ParseError) {
self.invalid.push(InvalidSuppression { kind, error });
}
}
/// Suppression for an unknown lint rule.
#[derive(Debug, PartialEq, Eq, get_size2::GetSize)]
struct UnknownSuppression {
/// The range of the code.
range: TextRange,
/// The range of the suppression comment
comment_range: TextRange,
reason: GetLintError,
}
#[derive(Debug, PartialEq, Eq, get_size2::GetSize)]
struct InvalidSuppression {
kind: SuppressionKind,
error: ParseError,
}