[red-knot] Silence unresolved-import in unreachable code (#17336)

## Summary

Similar to what we did for `unresolved-reference` and
`unresolved-attribute`, we now also silence `unresolved-import`
diagnostics if the corresponding `import` statement is unreachable.

This addresses the (already closed) issue #17049.

## Test Plan

Adapted Markdown tests.
This commit is contained in:
David Peter 2025-04-10 21:13:28 +02:00 committed by GitHub
parent 410aa4b8fd
commit 8b2727cf67
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
7 changed files with 131 additions and 103 deletions

View file

@ -10,8 +10,9 @@ use salsa::plumbing::AsId;
use salsa::Update;
use crate::module_name::ModuleName;
use crate::node_key::NodeKey;
use crate::semantic_index::ast_ids::node_key::ExpressionNodeKey;
use crate::semantic_index::ast_ids::{AstIds, ScopedExpressionId};
use crate::semantic_index::ast_ids::AstIds;
use crate::semantic_index::attribute_assignment::AttributeAssignments;
use crate::semantic_index::builder::SemanticIndexBuilder;
use crate::semantic_index::definition::{Definition, DefinitionNodeKey, Definitions};
@ -254,7 +255,7 @@ impl<'db> SemanticIndex<'db> {
})
}
/// Returns true if a given expression is reachable from the start of the scope. For example,
/// Returns true if a given AST node is reachable from the start of the scope. For example,
/// in the following code, expression `2` is reachable, but expressions `1` and `3` are not:
/// ```py
/// def f():
@ -265,16 +266,14 @@ impl<'db> SemanticIndex<'db> {
/// return
/// x # 3
/// ```
pub(crate) fn is_expression_reachable(
pub(crate) fn is_node_reachable(
&self,
db: &'db dyn crate::Db,
scope_id: FileScopeId,
expression_id: ScopedExpressionId,
node_key: NodeKey,
) -> bool {
self.is_scope_reachable(db, scope_id)
&& self
.use_def_map(scope_id)
.is_expression_reachable(db, expression_id)
&& self.use_def_map(scope_id).is_node_reachable(db, node_key)
}
/// Returns an iterator over the descendent scopes of `scope`.