-
Notifications
You must be signed in to change notification settings - Fork 903
/
ellipsis_in_non_empty_class_body.rs
76 lines (68 loc) · 2.14 KB
/
ellipsis_in_non_empty_class_body.rs
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
use ruff_diagnostics::{Diagnostic, Fix, FixKind, Violation};
use ruff_macros::{derive_message_formats, violation};
use ruff_python_ast::{Constant, Expr, ExprConstant, Stmt, StmtExpr};
use ruff_text_size::Ranged;
use crate::checkers::ast::Checker;
use crate::fix;
use crate::registry::AsRule;
/// ## What it does
/// Removes ellipses (`...`) in otherwise non-empty class bodies.
///
/// ## Why is this bad?
/// An ellipsis in a class body is only necessary if the class body is
/// otherwise empty. If the class body is non-empty, then the ellipsis
/// is redundant.
///
/// ## Example
/// ```python
/// class Foo:
/// ...
/// value: int
/// ```
///
/// Use instead:
/// ```python
/// class Foo:
/// value: int
/// ```
#[violation]
pub struct EllipsisInNonEmptyClassBody;
impl Violation for EllipsisInNonEmptyClassBody {
const FIX_KIND: FixKind = FixKind::Sometimes;
#[derive_message_formats]
fn message(&self) -> String {
format!("Non-empty class body must not contain `...`")
}
fn fix_title(&self) -> Option<String> {
Some("Remove unnecessary `...`".to_string())
}
}
/// PYI013
pub(crate) fn ellipsis_in_non_empty_class_body(checker: &mut Checker, body: &[Stmt]) {
// If the class body contains a single statement, then it's fine for it to be an ellipsis.
if body.len() == 1 {
return;
}
for stmt in body {
let Stmt::Expr(StmtExpr { value, .. }) = &stmt else {
continue;
};
if matches!(
value.as_ref(),
Expr::Constant(ExprConstant {
value: Constant::Ellipsis,
..
})
) {
let mut diagnostic = Diagnostic::new(EllipsisInNonEmptyClassBody, stmt.range());
if checker.patch(diagnostic.kind.rule()) {
let edit =
fix::edits::delete_stmt(stmt, Some(stmt), checker.locator(), checker.indexer());
diagnostic.set_fix(Fix::automatic(edit).isolate(Checker::isolation(
checker.semantic().current_statement_id(),
)));
}
checker.diagnostics.push(diagnostic);
}
}
}