Skip to content

Commit

Permalink
Add unknown-rule (astral-sh#15085)
Browse files Browse the repository at this point in the history
Co-authored-by: Carl Meyer <[email protected]>
  • Loading branch information
MichaReiser and carljm authored Dec 23, 2024
1 parent 68ada05 commit 2835d94
Show file tree
Hide file tree
Showing 5 changed files with 215 additions and 73 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -84,11 +84,10 @@ def test( # knot: ignore

## Can't suppress `revealed-type` diagnostics

TODO: Emit an error that the rule code is unknown: `unknown-rule`

```py
a = 10
# revealed: Literal[10]
# error: [unknown-rule] "Unknown rule `revealed-type`"
reveal_type(a) # knot: ignore[revealed-type]
```

Expand Down Expand Up @@ -164,3 +163,10 @@ severity: `knot: possibly-undefined-reference=error`

a = 4 / 0 # error: [division-by-zero]
```

## Unknown rule

```py
# error: [unknown-rule] "Unknown rule `is-equal-14`"
a = 10 + 4 # knot: ignore[is-equal-14]
```
3 changes: 2 additions & 1 deletion crates/red_knot_python_semantic/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use std::hash::BuildHasherDefault;
use rustc_hash::FxHasher;

use crate::lint::{LintRegistry, LintRegistryBuilder};
use crate::suppression::UNUSED_IGNORE_COMMENT;
use crate::suppression::{UNKNOWN_RULE, UNUSED_IGNORE_COMMENT};
pub use db::Db;
pub use module_name::ModuleName;
pub use module_resolver::{resolve_module, system_module_search_paths, KnownModule, Module};
Expand Down Expand Up @@ -49,4 +49,5 @@ pub fn default_lint_registry() -> &'static LintRegistry {
pub fn register_lints(registry: &mut LintRegistryBuilder) {
types::register_lints(registry);
registry.register_lint(&UNUSED_IGNORE_COMMENT);
registry.register_lint(&UNKNOWN_RULE);
}
7 changes: 6 additions & 1 deletion crates/red_knot_python_semantic/src/lint.rs
Original file line number Diff line number Diff line change
Expand Up @@ -374,7 +374,7 @@ impl LintRegistry {
}
}

#[derive(Error, Debug, Clone)]
#[derive(Error, Debug, Clone, PartialEq, Eq)]
pub enum GetLintError {
/// The name maps to this removed lint.
#[error("lint {0} has been removed")]
Expand Down Expand Up @@ -444,6 +444,11 @@ impl RuleSelection {
self.lints.get(&lint).copied()
}

/// Returns `true` if the `lint` is enabled.
pub fn is_enabled(&self, lint: LintId) -> bool {
self.severity(lint).is_some()
}

/// Enables `lint` and configures with the given `severity`.
///
/// Overrides any previous configuration for the lint.
Expand Down
Loading

0 comments on commit 2835d94

Please sign in to comment.