mirror of
https://github.com/astral-sh/ruff.git
synced 2025-09-30 05:44:56 +00:00
[ty] Avoid accessing class literal with incorrect AST (#18670)
Some checks are pending
CI / cargo clippy (push) Blocked by required conditions
CI / cargo build (msrv) (push) Blocked by required conditions
CI / Determine changes (push) Waiting to run
CI / cargo fmt (push) Waiting to run
CI / cargo test (linux) (push) Blocked by required conditions
CI / cargo test (linux, release) (push) Blocked by required conditions
CI / cargo test (windows) (push) Blocked by required conditions
CI / cargo test (wasm) (push) Blocked by required conditions
CI / cargo build (release) (push) Waiting to run
CI / cargo fuzz build (push) Blocked by required conditions
CI / fuzz parser (push) Blocked by required conditions
CI / test scripts (push) Blocked by required conditions
CI / ecosystem (push) Blocked by required conditions
CI / Fuzz for new ty panics (push) Blocked by required conditions
CI / cargo shear (push) Blocked by required conditions
CI / python package (push) Waiting to run
CI / pre-commit (push) Waiting to run
CI / mkdocs (push) Waiting to run
CI / formatter instabilities and black similarity (push) Blocked by required conditions
CI / test ruff-lsp (push) Blocked by required conditions
CI / check playground (push) Blocked by required conditions
CI / benchmarks (push) Blocked by required conditions
[ty Playground] Release / publish (push) Waiting to run
Some checks are pending
CI / cargo clippy (push) Blocked by required conditions
CI / cargo build (msrv) (push) Blocked by required conditions
CI / Determine changes (push) Waiting to run
CI / cargo fmt (push) Waiting to run
CI / cargo test (linux) (push) Blocked by required conditions
CI / cargo test (linux, release) (push) Blocked by required conditions
CI / cargo test (windows) (push) Blocked by required conditions
CI / cargo test (wasm) (push) Blocked by required conditions
CI / cargo build (release) (push) Waiting to run
CI / cargo fuzz build (push) Blocked by required conditions
CI / fuzz parser (push) Blocked by required conditions
CI / test scripts (push) Blocked by required conditions
CI / ecosystem (push) Blocked by required conditions
CI / Fuzz for new ty panics (push) Blocked by required conditions
CI / cargo shear (push) Blocked by required conditions
CI / python package (push) Waiting to run
CI / pre-commit (push) Waiting to run
CI / mkdocs (push) Waiting to run
CI / formatter instabilities and black similarity (push) Blocked by required conditions
CI / test ruff-lsp (push) Blocked by required conditions
CI / check playground (push) Blocked by required conditions
CI / benchmarks (push) Blocked by required conditions
[ty Playground] Release / publish (push) Waiting to run
This commit is contained in:
parent
e4423044f8
commit
5e02d839d5
2 changed files with 10 additions and 11 deletions
|
@ -2021,8 +2021,8 @@ impl<'db> ClassLiteral<'db> {
|
||||||
/// Returns a [`Span`] with the range of the class's header.
|
/// Returns a [`Span`] with the range of the class's header.
|
||||||
///
|
///
|
||||||
/// See [`Self::header_range`] for more details.
|
/// See [`Self::header_range`] for more details.
|
||||||
pub(super) fn header_span(self, db: &'db dyn Db, module: &ParsedModuleRef) -> Span {
|
pub(super) fn header_span(self, db: &'db dyn Db) -> Span {
|
||||||
Span::from(self.file(db)).with_range(self.header_range(db, module))
|
Span::from(self.file(db)).with_range(self.header_range(db))
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Returns the range of the class's "header": the class name
|
/// Returns the range of the class's "header": the class name
|
||||||
|
@ -2032,9 +2032,10 @@ impl<'db> ClassLiteral<'db> {
|
||||||
/// class Foo(Bar, metaclass=Baz): ...
|
/// class Foo(Bar, metaclass=Baz): ...
|
||||||
/// ^^^^^^^^^^^^^^^^^^^^^^^
|
/// ^^^^^^^^^^^^^^^^^^^^^^^
|
||||||
/// ```
|
/// ```
|
||||||
pub(super) fn header_range(self, db: &'db dyn Db, module: &ParsedModuleRef) -> TextRange {
|
pub(super) fn header_range(self, db: &'db dyn Db) -> TextRange {
|
||||||
let class_scope = self.body_scope(db);
|
let class_scope = self.body_scope(db);
|
||||||
let class_node = class_scope.node(db).expect_class(module);
|
let module = parsed_module(db.upcast(), class_scope.file(db)).load(db.upcast());
|
||||||
|
let class_node = class_scope.node(db).expect_class(&module);
|
||||||
let class_name = &class_node.name;
|
let class_name = &class_node.name;
|
||||||
TextRange::new(
|
TextRange::new(
|
||||||
class_name.start(),
|
class_name.start(),
|
||||||
|
|
|
@ -1788,7 +1788,7 @@ pub(super) fn report_implicit_return_type(
|
||||||
or `typing_extensions.Protocol` are considered protocol classes",
|
or `typing_extensions.Protocol` are considered protocol classes",
|
||||||
);
|
);
|
||||||
sub_diagnostic.annotate(
|
sub_diagnostic.annotate(
|
||||||
Annotation::primary(class.header_span(db, context.module())).message(format_args!(
|
Annotation::primary(class.header_span(db)).message(format_args!(
|
||||||
"`Protocol` not present in `{class}`'s immediate bases",
|
"`Protocol` not present in `{class}`'s immediate bases",
|
||||||
class = class.name(db)
|
class = class.name(db)
|
||||||
)),
|
)),
|
||||||
|
@ -1908,7 +1908,7 @@ pub(crate) fn report_bad_argument_to_get_protocol_members(
|
||||||
class.name(db)
|
class.name(db)
|
||||||
),
|
),
|
||||||
);
|
);
|
||||||
class_def_diagnostic.annotate(Annotation::primary(class.header_span(db, context.module())));
|
class_def_diagnostic.annotate(Annotation::primary(class.header_span(db)));
|
||||||
diagnostic.sub(class_def_diagnostic);
|
diagnostic.sub(class_def_diagnostic);
|
||||||
|
|
||||||
diagnostic.info(
|
diagnostic.info(
|
||||||
|
@ -1971,7 +1971,7 @@ pub(crate) fn report_runtime_check_against_non_runtime_checkable_protocol(
|
||||||
),
|
),
|
||||||
);
|
);
|
||||||
class_def_diagnostic.annotate(
|
class_def_diagnostic.annotate(
|
||||||
Annotation::primary(protocol.header_span(db, context.module()))
|
Annotation::primary(protocol.header_span(db))
|
||||||
.message(format_args!("`{class_name}` declared here")),
|
.message(format_args!("`{class_name}` declared here")),
|
||||||
);
|
);
|
||||||
diagnostic.sub(class_def_diagnostic);
|
diagnostic.sub(class_def_diagnostic);
|
||||||
|
@ -2002,7 +2002,7 @@ pub(crate) fn report_attempted_protocol_instantiation(
|
||||||
format_args!("Protocol classes cannot be instantiated"),
|
format_args!("Protocol classes cannot be instantiated"),
|
||||||
);
|
);
|
||||||
class_def_diagnostic.annotate(
|
class_def_diagnostic.annotate(
|
||||||
Annotation::primary(protocol.header_span(db, context.module()))
|
Annotation::primary(protocol.header_span(db))
|
||||||
.message(format_args!("`{class_name}` declared as a protocol here")),
|
.message(format_args!("`{class_name}` declared as a protocol here")),
|
||||||
);
|
);
|
||||||
diagnostic.sub(class_def_diagnostic);
|
diagnostic.sub(class_def_diagnostic);
|
||||||
|
@ -2016,9 +2016,7 @@ pub(crate) fn report_duplicate_bases(
|
||||||
) {
|
) {
|
||||||
let db = context.db();
|
let db = context.db();
|
||||||
|
|
||||||
let Some(builder) =
|
let Some(builder) = context.report_lint(&DUPLICATE_BASE, class.header_range(db)) else {
|
||||||
context.report_lint(&DUPLICATE_BASE, class.header_range(db, context.module()))
|
|
||||||
else {
|
|
||||||
return;
|
return;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue