Compare commits
1 Commits
zb/ignore-
...
charlie/bo
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
cc850ec348 |
8
.github/workflows/release.yml
vendored
8
.github/workflows/release.yml
vendored
@@ -60,7 +60,7 @@ jobs:
|
||||
env:
|
||||
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
|
||||
steps:
|
||||
- uses: actions/checkout@8e8c483db84b4bee98b60c0593521ed34d9990e8
|
||||
- uses: actions/checkout@1af3b93b6815bc44a9784bd300feb67ff0d1eeb3
|
||||
with:
|
||||
persist-credentials: false
|
||||
submodules: recursive
|
||||
@@ -123,7 +123,7 @@ jobs:
|
||||
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
|
||||
BUILD_MANIFEST_NAME: target/distrib/global-dist-manifest.json
|
||||
steps:
|
||||
- uses: actions/checkout@8e8c483db84b4bee98b60c0593521ed34d9990e8
|
||||
- uses: actions/checkout@1af3b93b6815bc44a9784bd300feb67ff0d1eeb3
|
||||
with:
|
||||
persist-credentials: false
|
||||
submodules: recursive
|
||||
@@ -174,7 +174,7 @@ jobs:
|
||||
outputs:
|
||||
val: ${{ steps.host.outputs.manifest }}
|
||||
steps:
|
||||
- uses: actions/checkout@8e8c483db84b4bee98b60c0593521ed34d9990e8
|
||||
- uses: actions/checkout@1af3b93b6815bc44a9784bd300feb67ff0d1eeb3
|
||||
with:
|
||||
persist-credentials: false
|
||||
submodules: recursive
|
||||
@@ -250,7 +250,7 @@ jobs:
|
||||
env:
|
||||
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
|
||||
steps:
|
||||
- uses: actions/checkout@8e8c483db84b4bee98b60c0593521ed34d9990e8
|
||||
- uses: actions/checkout@1af3b93b6815bc44a9784bd300feb67ff0d1eeb3
|
||||
with:
|
||||
persist-credentials: false
|
||||
submodules: recursive
|
||||
|
||||
42
CHANGELOG.md
42
CHANGELOG.md
@@ -1,47 +1,5 @@
|
||||
# Changelog
|
||||
|
||||
## 0.14.9
|
||||
|
||||
Released on 2025-12-11.
|
||||
|
||||
### Preview features
|
||||
|
||||
- \[`ruff`\] New `RUF100` diagnostics for unused range suppressions ([#21783](https://github.com/astral-sh/ruff/pull/21783))
|
||||
- \[`pylint`\] Detect subclasses of builtin exceptions (`PLW0133`) ([#21382](https://github.com/astral-sh/ruff/pull/21382))
|
||||
|
||||
### Bug fixes
|
||||
|
||||
- Fix comment placement in lambda parameters ([#21868](https://github.com/astral-sh/ruff/pull/21868))
|
||||
- Skip over trivia tokens after re-lexing ([#21895](https://github.com/astral-sh/ruff/pull/21895))
|
||||
- \[`flake8-bandit`\] Fix false positive when using non-standard `CSafeLoader` path (S506). ([#21830](https://github.com/astral-sh/ruff/pull/21830))
|
||||
- \[`flake8-bugbear`\] Accept immutable slice default arguments (`B008`) ([#21823](https://github.com/astral-sh/ruff/pull/21823))
|
||||
|
||||
### Rule changes
|
||||
|
||||
- \[`pydocstyle`\] Suppress `D417` for parameters with `Unpack` annotations ([#21816](https://github.com/astral-sh/ruff/pull/21816))
|
||||
|
||||
### Performance
|
||||
|
||||
- Use `memchr` for computing line indexes ([#21838](https://github.com/astral-sh/ruff/pull/21838))
|
||||
|
||||
### Documentation
|
||||
|
||||
- Document `*.pyw` is included by default in preview ([#21885](https://github.com/astral-sh/ruff/pull/21885))
|
||||
- Document range suppressions, reorganize suppression docs ([#21884](https://github.com/astral-sh/ruff/pull/21884))
|
||||
- Update mkdocs-material to 9.7.0 (Insiders now free) ([#21797](https://github.com/astral-sh/ruff/pull/21797))
|
||||
|
||||
### Contributors
|
||||
|
||||
- [@Avasam](https://github.com/Avasam)
|
||||
- [@MichaReiser](https://github.com/MichaReiser)
|
||||
- [@charliermarsh](https://github.com/charliermarsh)
|
||||
- [@amyreese](https://github.com/amyreese)
|
||||
- [@phongddo](https://github.com/phongddo)
|
||||
- [@prakhar1144](https://github.com/prakhar1144)
|
||||
- [@mahiro72](https://github.com/mahiro72)
|
||||
- [@ntBre](https://github.com/ntBre)
|
||||
- [@LoicRiegel](https://github.com/LoicRiegel)
|
||||
|
||||
## 0.14.8
|
||||
|
||||
Released on 2025-12-04.
|
||||
|
||||
63
Cargo.lock
generated
63
Cargo.lock
generated
@@ -254,21 +254,6 @@ dependencies = [
|
||||
"syn",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "bit-set"
|
||||
version = "0.8.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "08807e080ed7f9d5433fa9b275196cfc35414f66a0c79d864dc51a0d825231a3"
|
||||
dependencies = [
|
||||
"bit-vec",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "bit-vec"
|
||||
version = "0.8.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "5e764a1d40d510daf35e07be9eb06e75770908c27d411ee6c92109c9840eaaf7"
|
||||
|
||||
[[package]]
|
||||
name = "bitflags"
|
||||
version = "1.3.2"
|
||||
@@ -959,18 +944,6 @@ dependencies = [
|
||||
"parking_lot_core",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "datatest-stable"
|
||||
version = "0.3.3"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "a867d7322eb69cf3a68a5426387a25b45cb3b9c5ee41023ee6cea92e2afadd82"
|
||||
dependencies = [
|
||||
"camino",
|
||||
"fancy-regex",
|
||||
"libtest-mimic 0.8.1",
|
||||
"walkdir",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "derive-where"
|
||||
version = "1.6.0"
|
||||
@@ -1165,17 +1138,6 @@ dependencies = [
|
||||
"windows-sys 0.61.0",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "fancy-regex"
|
||||
version = "0.14.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "6e24cb5a94bcae1e5408b0effca5cd7172ea3c5755049c5f3af4cd283a165298"
|
||||
dependencies = [
|
||||
"bit-set",
|
||||
"regex-automata",
|
||||
"regex-syntax",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "fastrand"
|
||||
version = "2.3.0"
|
||||
@@ -1663,6 +1625,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "46fdb647ebde000f43b5b53f773c30cf9b0cb4300453208713fa38b2c70935a0"
|
||||
dependencies = [
|
||||
"console 0.15.11",
|
||||
"globset",
|
||||
"once_cell",
|
||||
"pest",
|
||||
"pest_derive",
|
||||
@@ -1670,6 +1633,7 @@ dependencies = [
|
||||
"ron",
|
||||
"serde",
|
||||
"similar",
|
||||
"walkdir",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
@@ -1955,18 +1919,6 @@ dependencies = [
|
||||
"threadpool",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "libtest-mimic"
|
||||
version = "0.8.1"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "5297962ef19edda4ce33aaa484386e0a5b3d7f2f4e037cbeee00503ef6b29d33"
|
||||
dependencies = [
|
||||
"anstream",
|
||||
"anstyle",
|
||||
"clap",
|
||||
"escape8259",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "linux-raw-sys"
|
||||
version = "0.11.0"
|
||||
@@ -2908,7 +2860,7 @@ dependencies = [
|
||||
|
||||
[[package]]
|
||||
name = "ruff"
|
||||
version = "0.14.9"
|
||||
version = "0.14.8"
|
||||
dependencies = [
|
||||
"anyhow",
|
||||
"argfile",
|
||||
@@ -3166,7 +3118,7 @@ dependencies = [
|
||||
|
||||
[[package]]
|
||||
name = "ruff_linter"
|
||||
version = "0.14.9"
|
||||
version = "0.14.8"
|
||||
dependencies = [
|
||||
"aho-corasick",
|
||||
"anyhow",
|
||||
@@ -3326,7 +3278,6 @@ dependencies = [
|
||||
"anyhow",
|
||||
"clap",
|
||||
"countme",
|
||||
"datatest-stable",
|
||||
"insta",
|
||||
"itertools 0.14.0",
|
||||
"memchr",
|
||||
@@ -3396,7 +3347,6 @@ dependencies = [
|
||||
"bitflags 2.10.0",
|
||||
"bstr",
|
||||
"compact_str",
|
||||
"datatest-stable",
|
||||
"get-size2",
|
||||
"insta",
|
||||
"itertools 0.14.0",
|
||||
@@ -3525,7 +3475,7 @@ dependencies = [
|
||||
|
||||
[[package]]
|
||||
name = "ruff_wasm"
|
||||
version = "0.14.9"
|
||||
version = "0.14.8"
|
||||
dependencies = [
|
||||
"console_error_panic_hook",
|
||||
"console_log",
|
||||
@@ -4361,7 +4311,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "5fe242ee9e646acec9ab73a5c540e8543ed1b107f0ce42be831e0775d423c396"
|
||||
dependencies = [
|
||||
"ignore",
|
||||
"libtest-mimic 0.7.3",
|
||||
"libtest-mimic",
|
||||
"snapbox",
|
||||
]
|
||||
|
||||
@@ -4390,7 +4340,6 @@ dependencies = [
|
||||
"ruff_python_trivia",
|
||||
"salsa",
|
||||
"tempfile",
|
||||
"tikv-jemallocator",
|
||||
"toml",
|
||||
"tracing",
|
||||
"tracing-flame",
|
||||
|
||||
@@ -5,7 +5,7 @@ resolver = "2"
|
||||
[workspace.package]
|
||||
# Please update rustfmt.toml when bumping the Rust edition
|
||||
edition = "2024"
|
||||
rust-version = "1.90"
|
||||
rust-version = "1.89"
|
||||
homepage = "https://docs.astral.sh/ruff"
|
||||
documentation = "https://docs.astral.sh/ruff"
|
||||
repository = "https://github.com/astral-sh/ruff"
|
||||
@@ -81,7 +81,6 @@ compact_str = "0.9.0"
|
||||
criterion = { version = "0.7.0", default-features = false }
|
||||
crossbeam = { version = "0.8.4" }
|
||||
dashmap = { version = "6.0.1" }
|
||||
datatest-stable = { version = "0.3.3" }
|
||||
dir-test = { version = "0.4.0" }
|
||||
dunce = { version = "1.0.5" }
|
||||
drop_bomb = { version = "0.1.5" }
|
||||
|
||||
13
README.md
13
README.md
@@ -57,11 +57,8 @@ Ruff is extremely actively developed and used in major open-source projects like
|
||||
|
||||
...and [many more](#whos-using-ruff).
|
||||
|
||||
Ruff is backed by [Astral](https://astral.sh), the creators of
|
||||
[uv](https://github.com/astral-sh/uv) and [ty](https://github.com/astral-sh/ty).
|
||||
|
||||
Read the [launch post](https://astral.sh/blog/announcing-astral-the-company-behind-ruff), or the
|
||||
original [project announcement](https://notes.crmarsh.com/python-tooling-could-be-much-much-faster).
|
||||
Ruff is backed by [Astral](https://astral.sh). Read the [launch post](https://astral.sh/blog/announcing-astral-the-company-behind-ruff),
|
||||
or the original [project announcement](https://notes.crmarsh.com/python-tooling-could-be-much-much-faster).
|
||||
|
||||
## Testimonials
|
||||
|
||||
@@ -150,8 +147,8 @@ curl -LsSf https://astral.sh/ruff/install.sh | sh
|
||||
powershell -c "irm https://astral.sh/ruff/install.ps1 | iex"
|
||||
|
||||
# For a specific version.
|
||||
curl -LsSf https://astral.sh/ruff/0.14.9/install.sh | sh
|
||||
powershell -c "irm https://astral.sh/ruff/0.14.9/install.ps1 | iex"
|
||||
curl -LsSf https://astral.sh/ruff/0.14.8/install.sh | sh
|
||||
powershell -c "irm https://astral.sh/ruff/0.14.8/install.ps1 | iex"
|
||||
```
|
||||
|
||||
You can also install Ruff via [Homebrew](https://formulae.brew.sh/formula/ruff), [Conda](https://anaconda.org/conda-forge/ruff),
|
||||
@@ -184,7 +181,7 @@ Ruff can also be used as a [pre-commit](https://pre-commit.com/) hook via [`ruff
|
||||
```yaml
|
||||
- repo: https://github.com/astral-sh/ruff-pre-commit
|
||||
# Ruff version.
|
||||
rev: v0.14.9
|
||||
rev: v0.14.8
|
||||
hooks:
|
||||
# Run the linter.
|
||||
- id: ruff-check
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
[package]
|
||||
name = "ruff"
|
||||
version = "0.14.9"
|
||||
version = "0.14.8"
|
||||
publish = true
|
||||
authors = { workspace = true }
|
||||
edition = { workspace = true }
|
||||
|
||||
@@ -10,7 +10,7 @@ use anyhow::bail;
|
||||
use clap::builder::Styles;
|
||||
use clap::builder::styling::{AnsiColor, Effects};
|
||||
use clap::builder::{TypedValueParser, ValueParserFactory};
|
||||
use clap::{Parser, Subcommand};
|
||||
use clap::{Parser, Subcommand, command};
|
||||
use colored::Colorize;
|
||||
use itertools::Itertools;
|
||||
use path_absolutize::path_dedot;
|
||||
|
||||
@@ -9,7 +9,7 @@ use std::sync::mpsc::channel;
|
||||
use anyhow::Result;
|
||||
use clap::CommandFactory;
|
||||
use colored::Colorize;
|
||||
use log::error;
|
||||
use log::{error, warn};
|
||||
use notify::{RecursiveMode, Watcher, recommended_watcher};
|
||||
|
||||
use args::{GlobalConfigArgs, ServerCommand};
|
||||
|
||||
@@ -194,7 +194,7 @@ static SYMPY: Benchmark = Benchmark::new(
|
||||
max_dep_date: "2025-06-17",
|
||||
python_version: PythonVersion::PY312,
|
||||
},
|
||||
13100,
|
||||
13030,
|
||||
);
|
||||
|
||||
static TANJUN: Benchmark = Benchmark::new(
|
||||
@@ -223,7 +223,7 @@ static STATIC_FRAME: Benchmark = Benchmark::new(
|
||||
max_dep_date: "2025-08-09",
|
||||
python_version: PythonVersion::PY311,
|
||||
},
|
||||
1100,
|
||||
950,
|
||||
);
|
||||
|
||||
#[track_caller]
|
||||
|
||||
@@ -144,8 +144,8 @@ fn emit_field(output: &mut String, name: &str, field: &OptionField, parents: &[S
|
||||
output.push('\n');
|
||||
|
||||
if let Some(deprecated) = &field.deprecated {
|
||||
output.push_str("!!! warning \"Deprecated\"\n");
|
||||
output.push_str(" This option has been deprecated");
|
||||
output.push_str("> [!WARN] \"Deprecated\"\n");
|
||||
output.push_str("> This option has been deprecated");
|
||||
|
||||
if let Some(since) = deprecated.since {
|
||||
write!(output, " in {since}").unwrap();
|
||||
@@ -166,9 +166,8 @@ fn emit_field(output: &mut String, name: &str, field: &OptionField, parents: &[S
|
||||
output.push('\n');
|
||||
let _ = writeln!(output, "**Type**: `{}`", field.value_type);
|
||||
output.push('\n');
|
||||
output.push_str("**Example usage**:\n\n");
|
||||
output.push_str("**Example usage** (`pyproject.toml`):\n\n");
|
||||
output.push_str(&format_example(
|
||||
"pyproject.toml",
|
||||
&format_header(
|
||||
field.scope,
|
||||
field.example,
|
||||
@@ -180,11 +179,11 @@ fn emit_field(output: &mut String, name: &str, field: &OptionField, parents: &[S
|
||||
output.push('\n');
|
||||
}
|
||||
|
||||
fn format_example(title: &str, header: &str, content: &str) -> String {
|
||||
fn format_example(header: &str, content: &str) -> String {
|
||||
if header.is_empty() {
|
||||
format!("```toml title=\"{title}\"\n{content}\n```\n",)
|
||||
format!("```toml\n{content}\n```\n",)
|
||||
} else {
|
||||
format!("```toml title=\"{title}\"\n{header}\n{content}\n```\n",)
|
||||
format!("```toml\n{header}\n{content}\n```\n",)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -39,7 +39,7 @@ impl Edit {
|
||||
|
||||
/// Creates an edit that replaces the content in `range` with `content`.
|
||||
pub fn range_replacement(content: String, range: TextRange) -> Self {
|
||||
debug_assert!(!content.is_empty(), "Prefer `Edit::deletion`");
|
||||
debug_assert!(!content.is_empty(), "Prefer `Fix::deletion`");
|
||||
|
||||
Self {
|
||||
content: Some(Box::from(content)),
|
||||
|
||||
@@ -337,7 +337,7 @@ macro_rules! best_fitting {
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use crate::prelude::*;
|
||||
use crate::{FormatState, SimpleFormatOptions, VecBuffer};
|
||||
use crate::{FormatState, SimpleFormatOptions, VecBuffer, write};
|
||||
|
||||
struct TestFormat;
|
||||
|
||||
@@ -385,8 +385,8 @@ mod tests {
|
||||
|
||||
#[test]
|
||||
fn best_fitting_variants_print_as_lists() {
|
||||
use crate::Formatted;
|
||||
use crate::prelude::*;
|
||||
use crate::{Formatted, format, format_args};
|
||||
|
||||
// The second variant below should be selected when printing at a width of 30
|
||||
let formatted_best_fitting = format!(
|
||||
|
||||
@@ -42,13 +42,14 @@ impl<'a> Collector<'a> {
|
||||
impl<'ast> SourceOrderVisitor<'ast> for Collector<'_> {
|
||||
fn visit_stmt(&mut self, stmt: &'ast Stmt) {
|
||||
match stmt {
|
||||
Stmt::ImportFrom(ast::StmtImportFrom {
|
||||
names,
|
||||
module,
|
||||
level,
|
||||
range: _,
|
||||
node_index: _,
|
||||
}) => {
|
||||
Stmt::ImportFrom(import_from) => {
|
||||
let ast::StmtImportFrom {
|
||||
names,
|
||||
module,
|
||||
level,
|
||||
range: _,
|
||||
node_index: _,
|
||||
} = &**import_from;
|
||||
let module = module.as_deref();
|
||||
let level = *level;
|
||||
for alias in names {
|
||||
@@ -87,24 +88,26 @@ impl<'ast> SourceOrderVisitor<'ast> for Collector<'_> {
|
||||
}
|
||||
}
|
||||
}
|
||||
Stmt::Import(ast::StmtImport {
|
||||
names,
|
||||
range: _,
|
||||
node_index: _,
|
||||
}) => {
|
||||
Stmt::Import(import_stmt) => {
|
||||
let ast::StmtImport {
|
||||
names,
|
||||
range: _,
|
||||
node_index: _,
|
||||
} = &**import_stmt;
|
||||
for alias in names {
|
||||
if let Some(module_name) = ModuleName::new(alias.name.as_str()) {
|
||||
self.imports.push(CollectedImport::Import(module_name));
|
||||
}
|
||||
}
|
||||
}
|
||||
Stmt::If(ast::StmtIf {
|
||||
test,
|
||||
body,
|
||||
elif_else_clauses,
|
||||
range: _,
|
||||
node_index: _,
|
||||
}) => {
|
||||
Stmt::If(if_stmt) => {
|
||||
let ast::StmtIf {
|
||||
test,
|
||||
body,
|
||||
elif_else_clauses,
|
||||
range: _,
|
||||
node_index: _,
|
||||
} = &**if_stmt;
|
||||
// Skip TYPE_CHECKING blocks if not requested
|
||||
if self.type_checking_imports || !is_type_checking_condition(test) {
|
||||
self.visit_body(body);
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
[package]
|
||||
name = "ruff_linter"
|
||||
version = "0.14.9"
|
||||
version = "0.14.8"
|
||||
publish = false
|
||||
authors = { workspace = true }
|
||||
edition = { workspace = true }
|
||||
|
||||
@@ -132,6 +132,7 @@ async def c():
|
||||
# Non-errors
|
||||
###
|
||||
|
||||
# False-negative: RustPython doesn't parse the `\N{snowman}`.
|
||||
"\N{snowman} {}".format(a)
|
||||
|
||||
"{".format(a)
|
||||
@@ -275,6 +276,3 @@ if __name__ == "__main__":
|
||||
number = 0
|
||||
string = "{}".format(number := number + 1)
|
||||
print(string)
|
||||
|
||||
# Unicode escape
|
||||
"\N{angle}AOB = {angle}°".format(angle=180)
|
||||
|
||||
@@ -1,38 +0,0 @@
|
||||
a: int = 1
|
||||
def f1():
|
||||
global a
|
||||
a: str = "foo" # error
|
||||
|
||||
b: int = 1
|
||||
def outer():
|
||||
def inner():
|
||||
global b
|
||||
b: str = "nested" # error
|
||||
|
||||
c: int = 1
|
||||
def f2():
|
||||
global c
|
||||
c: list[str] = [] # error
|
||||
|
||||
d: int = 1
|
||||
def f3():
|
||||
global d
|
||||
d: str # error
|
||||
|
||||
e: int = 1
|
||||
def f4():
|
||||
e: str = "happy" # okay
|
||||
|
||||
global f
|
||||
f: int = 1 # okay
|
||||
|
||||
g: int = 1
|
||||
global g # error
|
||||
|
||||
class C:
|
||||
x: str
|
||||
global x # error
|
||||
|
||||
class D:
|
||||
global x # error
|
||||
x: str
|
||||
@@ -17,11 +17,12 @@ use ruff_python_ast::PythonVersion;
|
||||
/// Run lint rules over a [`Stmt`] syntax node.
|
||||
pub(crate) fn statement(stmt: &Stmt, checker: &mut Checker) {
|
||||
match stmt {
|
||||
Stmt::Global(ast::StmtGlobal {
|
||||
names,
|
||||
range: _,
|
||||
node_index: _,
|
||||
}) => {
|
||||
Stmt::Global(global) => {
|
||||
let ast::StmtGlobal {
|
||||
names,
|
||||
range: _,
|
||||
node_index: _,
|
||||
} = &**global;
|
||||
if checker.is_rule_enabled(Rule::GlobalAtModuleLevel) {
|
||||
pylint::rules::global_at_module_level(checker, stmt);
|
||||
}
|
||||
@@ -31,13 +32,12 @@ pub(crate) fn statement(stmt: &Stmt, checker: &mut Checker) {
|
||||
}
|
||||
}
|
||||
}
|
||||
Stmt::Nonlocal(
|
||||
nonlocal @ ast::StmtNonlocal {
|
||||
Stmt::Nonlocal(nonlocal) => {
|
||||
let ast::StmtNonlocal {
|
||||
names,
|
||||
range: _,
|
||||
node_index: _,
|
||||
},
|
||||
) => {
|
||||
} = &**nonlocal;
|
||||
if checker.is_rule_enabled(Rule::AmbiguousVariableName) {
|
||||
for name in names {
|
||||
pycodestyle::rules::ambiguous_variable_name(checker, name, name.range());
|
||||
@@ -47,8 +47,8 @@ pub(crate) fn statement(stmt: &Stmt, checker: &mut Checker) {
|
||||
pylint::rules::nonlocal_and_global(checker, nonlocal);
|
||||
}
|
||||
}
|
||||
Stmt::FunctionDef(
|
||||
function_def @ ast::StmtFunctionDef {
|
||||
Stmt::FunctionDef(function_def) => {
|
||||
let ast::StmtFunctionDef {
|
||||
is_async,
|
||||
name,
|
||||
decorator_list,
|
||||
@@ -58,8 +58,7 @@ pub(crate) fn statement(stmt: &Stmt, checker: &mut Checker) {
|
||||
type_params: _,
|
||||
range: _,
|
||||
node_index: _,
|
||||
},
|
||||
) => {
|
||||
} = &**function_def;
|
||||
if checker.is_rule_enabled(Rule::DjangoNonLeadingReceiverDecorator) {
|
||||
flake8_django::rules::non_leading_receiver_decorator(checker, decorator_list);
|
||||
}
|
||||
@@ -321,7 +320,7 @@ pub(crate) fn statement(stmt: &Stmt, checker: &mut Checker) {
|
||||
pylint::rules::in_function(checker, name, body);
|
||||
}
|
||||
if checker.is_rule_enabled(Rule::ReimplementedOperator) {
|
||||
refurb::rules::reimplemented_operator(checker, &function_def.into());
|
||||
refurb::rules::reimplemented_operator(checker, &(&**function_def).into());
|
||||
}
|
||||
if checker.is_rule_enabled(Rule::SslWithBadDefaults) {
|
||||
flake8_bandit::rules::ssl_with_bad_defaults(checker, function_def);
|
||||
@@ -356,8 +355,8 @@ pub(crate) fn statement(stmt: &Stmt, checker: &mut Checker) {
|
||||
pylint::rules::return_in_init(checker, stmt);
|
||||
}
|
||||
}
|
||||
Stmt::ClassDef(
|
||||
class_def @ ast::StmtClassDef {
|
||||
Stmt::ClassDef(class_def) => {
|
||||
let ast::StmtClassDef {
|
||||
name,
|
||||
arguments,
|
||||
type_params: _,
|
||||
@@ -365,8 +364,7 @@ pub(crate) fn statement(stmt: &Stmt, checker: &mut Checker) {
|
||||
body,
|
||||
range: _,
|
||||
node_index: _,
|
||||
},
|
||||
) => {
|
||||
} = &**class_def;
|
||||
if checker.is_rule_enabled(Rule::NoClassmethodDecorator) {
|
||||
pylint::rules::no_classmethod_decorator(checker, stmt);
|
||||
}
|
||||
@@ -526,11 +524,12 @@ pub(crate) fn statement(stmt: &Stmt, checker: &mut Checker) {
|
||||
ruff::rules::implicit_class_var_in_dataclass(checker, class_def);
|
||||
}
|
||||
}
|
||||
Stmt::Import(ast::StmtImport {
|
||||
names,
|
||||
range: _,
|
||||
node_index: _,
|
||||
}) => {
|
||||
Stmt::Import(import) => {
|
||||
let ast::StmtImport {
|
||||
names,
|
||||
range: _,
|
||||
node_index: _,
|
||||
} = &**import;
|
||||
if checker.is_rule_enabled(Rule::MultipleImportsOnOneLine) {
|
||||
pycodestyle::rules::multiple_imports_on_one_line(checker, stmt, names);
|
||||
}
|
||||
@@ -578,7 +577,7 @@ pub(crate) fn statement(stmt: &Stmt, checker: &mut Checker) {
|
||||
flake8_tidy_imports::rules::banned_module_level_imports(checker, stmt);
|
||||
}
|
||||
|
||||
for alias in names {
|
||||
for alias in &import.names {
|
||||
if checker.is_rule_enabled(Rule::NonAsciiImportName) {
|
||||
pylint::rules::non_ascii_module_import(checker, alias);
|
||||
}
|
||||
@@ -604,7 +603,7 @@ pub(crate) fn statement(stmt: &Stmt, checker: &mut Checker) {
|
||||
}
|
||||
}
|
||||
if checker.is_rule_enabled(Rule::ManualFromImport) {
|
||||
pylint::rules::manual_from_import(checker, stmt, alias, names);
|
||||
pylint::rules::manual_from_import(checker, stmt, alias, &import.names);
|
||||
}
|
||||
if checker.is_rule_enabled(Rule::ImportSelf) {
|
||||
pylint::rules::import_self(checker, alias, checker.module.qualified_name());
|
||||
@@ -681,17 +680,9 @@ pub(crate) fn statement(stmt: &Stmt, checker: &mut Checker) {
|
||||
}
|
||||
}
|
||||
}
|
||||
Stmt::ImportFrom(
|
||||
import_from @ ast::StmtImportFrom {
|
||||
names,
|
||||
module,
|
||||
level,
|
||||
range: _,
|
||||
node_index: _,
|
||||
},
|
||||
) => {
|
||||
let level = *level;
|
||||
let module = module.as_deref();
|
||||
Stmt::ImportFrom(import_from) => {
|
||||
let level = import_from.level;
|
||||
let module = import_from.module.as_deref();
|
||||
if checker.is_rule_enabled(Rule::ModuleImportNotAtTopOfFile) {
|
||||
pycodestyle::rules::module_import_not_at_top_of_file(checker, stmt);
|
||||
}
|
||||
@@ -699,7 +690,7 @@ pub(crate) fn statement(stmt: &Stmt, checker: &mut Checker) {
|
||||
pylint::rules::import_outside_top_level(checker, stmt);
|
||||
}
|
||||
if checker.is_rule_enabled(Rule::GlobalStatement) {
|
||||
for name in names {
|
||||
for name in &import_from.names {
|
||||
if let Some(asname) = name.asname.as_ref() {
|
||||
pylint::rules::global_statement(checker, asname);
|
||||
} else {
|
||||
@@ -708,7 +699,7 @@ pub(crate) fn statement(stmt: &Stmt, checker: &mut Checker) {
|
||||
}
|
||||
}
|
||||
if checker.is_rule_enabled(Rule::NonAsciiImportName) {
|
||||
for alias in names {
|
||||
for alias in &import_from.names {
|
||||
pylint::rules::non_ascii_module_import(checker, alias);
|
||||
}
|
||||
}
|
||||
@@ -724,7 +715,7 @@ pub(crate) fn statement(stmt: &Stmt, checker: &mut Checker) {
|
||||
if checker.is_rule_enabled(Rule::UnnecessaryBuiltinImport) {
|
||||
if let Some(module) = module {
|
||||
pyupgrade::rules::unnecessary_builtin_import(
|
||||
checker, stmt, module, names, level,
|
||||
checker, stmt, module, &import_from.names, level,
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -760,7 +751,7 @@ pub(crate) fn statement(stmt: &Stmt, checker: &mut Checker) {
|
||||
&stmt,
|
||||
);
|
||||
|
||||
for alias in names {
|
||||
for alias in &import_from.names {
|
||||
if &alias.name == "*" {
|
||||
continue;
|
||||
}
|
||||
@@ -789,7 +780,7 @@ pub(crate) fn statement(stmt: &Stmt, checker: &mut Checker) {
|
||||
flake8_pyi::rules::from_future_import(checker, import_from);
|
||||
}
|
||||
}
|
||||
for alias in names {
|
||||
for alias in &import_from.names {
|
||||
if module != Some("__future__") && &alias.name == "*" {
|
||||
// F403
|
||||
checker.report_diagnostic_if_enabled(
|
||||
@@ -890,7 +881,7 @@ pub(crate) fn statement(stmt: &Stmt, checker: &mut Checker) {
|
||||
checker,
|
||||
level,
|
||||
module,
|
||||
names,
|
||||
&import_from.names,
|
||||
checker.module.qualified_name(),
|
||||
);
|
||||
}
|
||||
@@ -906,14 +897,14 @@ pub(crate) fn statement(stmt: &Stmt, checker: &mut Checker) {
|
||||
flake8_pyi::rules::bytestring_import(checker, import_from);
|
||||
}
|
||||
}
|
||||
Stmt::Raise(raise @ ast::StmtRaise { exc, .. }) => {
|
||||
Stmt::Raise(raise) => {
|
||||
if checker.is_rule_enabled(Rule::RaiseNotImplemented) {
|
||||
if let Some(expr) = exc {
|
||||
if let Some(expr) = &raise.exc {
|
||||
pyflakes::rules::raise_not_implemented(checker, expr);
|
||||
}
|
||||
}
|
||||
if checker.is_rule_enabled(Rule::RaiseLiteral) {
|
||||
if let Some(exc) = exc {
|
||||
if let Some(exc) = &raise.exc {
|
||||
flake8_bugbear::rules::raise_literal(checker, exc);
|
||||
}
|
||||
}
|
||||
@@ -922,34 +913,34 @@ pub(crate) fn statement(stmt: &Stmt, checker: &mut Checker) {
|
||||
Rule::FStringInException,
|
||||
Rule::DotFormatInException,
|
||||
]) {
|
||||
if let Some(exc) = exc {
|
||||
if let Some(exc) = &raise.exc {
|
||||
flake8_errmsg::rules::string_in_exception(checker, stmt, exc);
|
||||
}
|
||||
}
|
||||
if checker.is_rule_enabled(Rule::OSErrorAlias) {
|
||||
if let Some(item) = exc {
|
||||
if let Some(item) = &raise.exc {
|
||||
pyupgrade::rules::os_error_alias_raise(checker, item);
|
||||
}
|
||||
}
|
||||
if checker.is_rule_enabled(Rule::TimeoutErrorAlias) {
|
||||
if checker.target_version() >= PythonVersion::PY310 {
|
||||
if let Some(item) = exc {
|
||||
if let Some(item) = &raise.exc {
|
||||
pyupgrade::rules::timeout_error_alias_raise(checker, item);
|
||||
}
|
||||
}
|
||||
}
|
||||
if checker.is_rule_enabled(Rule::RaiseVanillaClass) {
|
||||
if let Some(expr) = exc {
|
||||
if let Some(expr) = &raise.exc {
|
||||
tryceratops::rules::raise_vanilla_class(checker, expr);
|
||||
}
|
||||
}
|
||||
if checker.is_rule_enabled(Rule::RaiseVanillaArgs) {
|
||||
if let Some(expr) = exc {
|
||||
if let Some(expr) = &raise.exc {
|
||||
tryceratops::rules::raise_vanilla_args(checker, expr);
|
||||
}
|
||||
}
|
||||
if checker.is_rule_enabled(Rule::UnnecessaryParenOnRaiseException) {
|
||||
if let Some(expr) = exc {
|
||||
if let Some(expr) = &raise.exc {
|
||||
flake8_raise::rules::unnecessary_paren_on_raise_exception(checker, expr);
|
||||
}
|
||||
}
|
||||
@@ -957,9 +948,9 @@ pub(crate) fn statement(stmt: &Stmt, checker: &mut Checker) {
|
||||
pylint::rules::misplaced_bare_raise(checker, raise);
|
||||
}
|
||||
}
|
||||
Stmt::AugAssign(aug_assign @ ast::StmtAugAssign { target, .. }) => {
|
||||
Stmt::AugAssign(aug_assign) => {
|
||||
if checker.is_rule_enabled(Rule::GlobalStatement) {
|
||||
if let Expr::Name(ast::ExprName { id, .. }) = target.as_ref() {
|
||||
if let Expr::Name(ast::ExprName { id, .. }) = aug_assign.target.as_ref() {
|
||||
pylint::rules::global_statement(checker, id);
|
||||
}
|
||||
}
|
||||
@@ -967,13 +958,7 @@ pub(crate) fn statement(stmt: &Stmt, checker: &mut Checker) {
|
||||
ruff::rules::sort_dunder_all_aug_assign(checker, aug_assign);
|
||||
}
|
||||
}
|
||||
Stmt::If(
|
||||
if_ @ ast::StmtIf {
|
||||
test,
|
||||
elif_else_clauses,
|
||||
..
|
||||
},
|
||||
) => {
|
||||
Stmt::If(if_) => {
|
||||
if checker.is_rule_enabled(Rule::TooManyNestedBlocks) {
|
||||
pylint::rules::too_many_nested_blocks(checker, stmt);
|
||||
}
|
||||
@@ -1036,33 +1021,33 @@ pub(crate) fn statement(stmt: &Stmt, checker: &mut Checker) {
|
||||
Rule::PatchVersionComparison,
|
||||
Rule::WrongTupleLengthVersionComparison,
|
||||
]) {
|
||||
if let Expr::BoolOp(ast::ExprBoolOp { values, .. }) = test.as_ref() {
|
||||
if let Expr::BoolOp(ast::ExprBoolOp { values, .. }) = if_.test.as_ref() {
|
||||
for value in values {
|
||||
flake8_pyi::rules::unrecognized_version_info(checker, value);
|
||||
}
|
||||
} else {
|
||||
flake8_pyi::rules::unrecognized_version_info(checker, test);
|
||||
flake8_pyi::rules::unrecognized_version_info(checker, &if_.test);
|
||||
}
|
||||
}
|
||||
if checker.any_rule_enabled(&[
|
||||
Rule::UnrecognizedPlatformCheck,
|
||||
Rule::UnrecognizedPlatformName,
|
||||
]) {
|
||||
if let Expr::BoolOp(ast::ExprBoolOp { values, .. }) = test.as_ref() {
|
||||
if let Expr::BoolOp(ast::ExprBoolOp { values, .. }) = if_.test.as_ref() {
|
||||
for value in values {
|
||||
flake8_pyi::rules::unrecognized_platform(checker, value);
|
||||
}
|
||||
} else {
|
||||
flake8_pyi::rules::unrecognized_platform(checker, test);
|
||||
flake8_pyi::rules::unrecognized_platform(checker, &if_.test);
|
||||
}
|
||||
}
|
||||
if checker.is_rule_enabled(Rule::ComplexIfStatementInStub) {
|
||||
if let Expr::BoolOp(ast::ExprBoolOp { values, .. }) = test.as_ref() {
|
||||
if let Expr::BoolOp(ast::ExprBoolOp { values, .. }) = if_.test.as_ref() {
|
||||
for value in values {
|
||||
flake8_pyi::rules::complex_if_statement_in_stub(checker, value);
|
||||
}
|
||||
} else {
|
||||
flake8_pyi::rules::complex_if_statement_in_stub(checker, test);
|
||||
flake8_pyi::rules::complex_if_statement_in_stub(checker, &if_.test);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1091,10 +1076,10 @@ pub(crate) fn statement(stmt: &Stmt, checker: &mut Checker) {
|
||||
}
|
||||
}
|
||||
|
||||
let has_else_clause = elif_else_clauses.iter().any(|clause| clause.test.is_none());
|
||||
let has_else_clause = if_.elif_else_clauses.iter().any(|clause| clause.test.is_none());
|
||||
|
||||
bad_version_info_comparison(checker, test.as_ref(), has_else_clause);
|
||||
for clause in elif_else_clauses {
|
||||
bad_version_info_comparison(checker, if_.test.as_ref(), has_else_clause);
|
||||
for clause in &if_.elif_else_clauses {
|
||||
if let Some(test) = clause.test.as_ref() {
|
||||
bad_version_info_comparison(checker, test, has_else_clause);
|
||||
}
|
||||
@@ -1105,44 +1090,37 @@ pub(crate) fn statement(stmt: &Stmt, checker: &mut Checker) {
|
||||
ruff::rules::if_key_in_dict_del(checker, if_);
|
||||
}
|
||||
if checker.is_rule_enabled(Rule::NeedlessElse) {
|
||||
ruff::rules::needless_else(checker, if_.into());
|
||||
ruff::rules::needless_else(checker, (&**if_).into());
|
||||
}
|
||||
}
|
||||
Stmt::Assert(
|
||||
assert_stmt @ ast::StmtAssert {
|
||||
test,
|
||||
msg,
|
||||
range: _,
|
||||
node_index: _,
|
||||
},
|
||||
) => {
|
||||
Stmt::Assert(assert_stmt) => {
|
||||
if !checker.semantic.in_type_checking_block() {
|
||||
if checker.is_rule_enabled(Rule::Assert) {
|
||||
flake8_bandit::rules::assert_used(checker, stmt);
|
||||
}
|
||||
}
|
||||
if checker.is_rule_enabled(Rule::AssertTuple) {
|
||||
pyflakes::rules::assert_tuple(checker, stmt, test);
|
||||
pyflakes::rules::assert_tuple(checker, stmt, &assert_stmt.test);
|
||||
}
|
||||
if checker.is_rule_enabled(Rule::AssertFalse) {
|
||||
flake8_bugbear::rules::assert_false(checker, stmt, test, msg.as_deref());
|
||||
flake8_bugbear::rules::assert_false(checker, stmt, &assert_stmt.test, assert_stmt.msg.as_deref());
|
||||
}
|
||||
if checker.is_rule_enabled(Rule::PytestAssertAlwaysFalse) {
|
||||
flake8_pytest_style::rules::assert_falsy(checker, stmt, test);
|
||||
flake8_pytest_style::rules::assert_falsy(checker, stmt, &assert_stmt.test);
|
||||
}
|
||||
if checker.is_rule_enabled(Rule::PytestCompositeAssertion) {
|
||||
flake8_pytest_style::rules::composite_condition(
|
||||
checker,
|
||||
stmt,
|
||||
test,
|
||||
msg.as_deref(),
|
||||
&assert_stmt.test,
|
||||
assert_stmt.msg.as_deref(),
|
||||
);
|
||||
}
|
||||
if checker.is_rule_enabled(Rule::AssertOnStringLiteral) {
|
||||
pylint::rules::assert_on_string_literal(checker, test);
|
||||
pylint::rules::assert_on_string_literal(checker, &assert_stmt.test);
|
||||
}
|
||||
if checker.is_rule_enabled(Rule::InvalidMockAccess) {
|
||||
pygrep_hooks::rules::non_existent_mock_method(checker, test);
|
||||
pygrep_hooks::rules::non_existent_mock_method(checker, &assert_stmt.test);
|
||||
}
|
||||
if checker.is_rule_enabled(Rule::AssertWithPrintMessage) {
|
||||
ruff::rules::assert_with_print_message(checker, assert_stmt);
|
||||
@@ -1151,18 +1129,18 @@ pub(crate) fn statement(stmt: &Stmt, checker: &mut Checker) {
|
||||
ruff::rules::invalid_assert_message_literal_argument(checker, assert_stmt);
|
||||
}
|
||||
}
|
||||
Stmt::With(with_stmt @ ast::StmtWith { items, body, .. }) => {
|
||||
Stmt::With(with_stmt) => {
|
||||
if checker.is_rule_enabled(Rule::TooManyNestedBlocks) {
|
||||
pylint::rules::too_many_nested_blocks(checker, stmt);
|
||||
}
|
||||
if checker.is_rule_enabled(Rule::AssertRaisesException) {
|
||||
flake8_bugbear::rules::assert_raises_exception(checker, items);
|
||||
flake8_bugbear::rules::assert_raises_exception(checker, &with_stmt.items);
|
||||
}
|
||||
if checker.is_rule_enabled(Rule::PytestRaisesWithMultipleStatements) {
|
||||
flake8_pytest_style::rules::complex_raises(checker, stmt, items, body);
|
||||
flake8_pytest_style::rules::complex_raises(checker, stmt, &with_stmt.items, &with_stmt.body);
|
||||
}
|
||||
if checker.is_rule_enabled(Rule::PytestWarnsWithMultipleStatements) {
|
||||
flake8_pytest_style::rules::complex_warns(checker, stmt, items, body);
|
||||
flake8_pytest_style::rules::complex_warns(checker, stmt, &with_stmt.items, &with_stmt.body);
|
||||
}
|
||||
if checker.is_rule_enabled(Rule::MultipleWithStatements) {
|
||||
flake8_simplify::rules::multiple_with_statements(
|
||||
@@ -1184,10 +1162,10 @@ pub(crate) fn statement(stmt: &Stmt, checker: &mut Checker) {
|
||||
pylint::rules::useless_with_lock(checker, with_stmt);
|
||||
}
|
||||
if checker.is_rule_enabled(Rule::CancelScopeNoCheckpoint) {
|
||||
flake8_async::rules::cancel_scope_no_checkpoint(checker, with_stmt, items);
|
||||
flake8_async::rules::cancel_scope_no_checkpoint(checker, with_stmt, &with_stmt.items);
|
||||
}
|
||||
}
|
||||
Stmt::While(while_stmt @ ast::StmtWhile { body, orelse, .. }) => {
|
||||
Stmt::While(while_stmt) => {
|
||||
if checker.is_rule_enabled(Rule::TooManyNestedBlocks) {
|
||||
pylint::rules::too_many_nested_blocks(checker, stmt);
|
||||
}
|
||||
@@ -1195,29 +1173,19 @@ pub(crate) fn statement(stmt: &Stmt, checker: &mut Checker) {
|
||||
flake8_bugbear::rules::function_uses_loop_variable(checker, &Node::Stmt(stmt));
|
||||
}
|
||||
if checker.is_rule_enabled(Rule::UselessElseOnLoop) {
|
||||
pylint::rules::useless_else_on_loop(checker, stmt, body, orelse);
|
||||
pylint::rules::useless_else_on_loop(checker, stmt, &while_stmt.body, &while_stmt.orelse);
|
||||
}
|
||||
if checker.is_rule_enabled(Rule::TryExceptInLoop) {
|
||||
perflint::rules::try_except_in_loop(checker, body);
|
||||
perflint::rules::try_except_in_loop(checker, &while_stmt.body);
|
||||
}
|
||||
if checker.is_rule_enabled(Rule::AsyncBusyWait) {
|
||||
flake8_async::rules::async_busy_wait(checker, while_stmt);
|
||||
}
|
||||
if checker.is_rule_enabled(Rule::NeedlessElse) {
|
||||
ruff::rules::needless_else(checker, while_stmt.into());
|
||||
ruff::rules::needless_else(checker, (&**while_stmt).into());
|
||||
}
|
||||
}
|
||||
Stmt::For(
|
||||
for_stmt @ ast::StmtFor {
|
||||
target,
|
||||
body,
|
||||
iter,
|
||||
orelse,
|
||||
is_async,
|
||||
range: _,
|
||||
node_index: _,
|
||||
},
|
||||
) => {
|
||||
Stmt::For(for_stmt) => {
|
||||
if checker.is_rule_enabled(Rule::TooManyNestedBlocks) {
|
||||
pylint::rules::too_many_nested_blocks(checker, stmt);
|
||||
}
|
||||
@@ -1235,25 +1203,25 @@ pub(crate) fn statement(stmt: &Stmt, checker: &mut Checker) {
|
||||
checker.analyze.for_loops.push(checker.semantic.snapshot());
|
||||
}
|
||||
if checker.is_rule_enabled(Rule::LoopVariableOverridesIterator) {
|
||||
flake8_bugbear::rules::loop_variable_overrides_iterator(checker, target, iter);
|
||||
flake8_bugbear::rules::loop_variable_overrides_iterator(checker, &for_stmt.target, &for_stmt.iter);
|
||||
}
|
||||
if checker.is_rule_enabled(Rule::FunctionUsesLoopVariable) {
|
||||
flake8_bugbear::rules::function_uses_loop_variable(checker, &Node::Stmt(stmt));
|
||||
}
|
||||
if checker.is_rule_enabled(Rule::ReuseOfGroupbyGenerator) {
|
||||
flake8_bugbear::rules::reuse_of_groupby_generator(checker, target, body, iter);
|
||||
flake8_bugbear::rules::reuse_of_groupby_generator(checker, &for_stmt.target, &for_stmt.body, &for_stmt.iter);
|
||||
}
|
||||
if checker.is_rule_enabled(Rule::UselessElseOnLoop) {
|
||||
pylint::rules::useless_else_on_loop(checker, stmt, body, orelse);
|
||||
pylint::rules::useless_else_on_loop(checker, stmt, &for_stmt.body, &for_stmt.orelse);
|
||||
}
|
||||
if checker.is_rule_enabled(Rule::RedefinedLoopName) {
|
||||
pylint::rules::redefined_loop_name(checker, stmt);
|
||||
}
|
||||
if checker.is_rule_enabled(Rule::IterationOverSet) {
|
||||
pylint::rules::iteration_over_set(checker, iter);
|
||||
pylint::rules::iteration_over_set(checker, &for_stmt.iter);
|
||||
}
|
||||
if checker.is_rule_enabled(Rule::DictIterMissingItems) {
|
||||
pylint::rules::dict_iter_missing_items(checker, target, iter);
|
||||
pylint::rules::dict_iter_missing_items(checker, &for_stmt.target, &for_stmt.iter);
|
||||
}
|
||||
if checker.is_rule_enabled(Rule::ManualListCopy) {
|
||||
perflint::rules::manual_list_copy(checker, for_stmt);
|
||||
@@ -1263,7 +1231,7 @@ pub(crate) fn statement(stmt: &Stmt, checker: &mut Checker) {
|
||||
pylint::rules::modified_iterating_set(checker, for_stmt);
|
||||
}
|
||||
if checker.is_rule_enabled(Rule::UnnecessaryListCast) {
|
||||
perflint::rules::unnecessary_list_cast(checker, iter, body);
|
||||
perflint::rules::unnecessary_list_cast(checker, &for_stmt.iter, &for_stmt.body);
|
||||
}
|
||||
if checker.is_rule_enabled(Rule::UnnecessaryListIndexLookup) {
|
||||
pylint::rules::unnecessary_list_index_lookup(checker, for_stmt);
|
||||
@@ -1274,7 +1242,7 @@ pub(crate) fn statement(stmt: &Stmt, checker: &mut Checker) {
|
||||
if checker.is_rule_enabled(Rule::ReadlinesInFor) {
|
||||
refurb::rules::readlines_in_for(checker, for_stmt);
|
||||
}
|
||||
if !*is_async {
|
||||
if !for_stmt.is_async {
|
||||
if checker.is_rule_enabled(Rule::ReimplementedBuiltin) {
|
||||
flake8_simplify::rules::convert_for_loop_to_any_all(checker, stmt);
|
||||
}
|
||||
@@ -1282,7 +1250,7 @@ pub(crate) fn statement(stmt: &Stmt, checker: &mut Checker) {
|
||||
flake8_simplify::rules::key_in_dict_for(checker, for_stmt);
|
||||
}
|
||||
if checker.is_rule_enabled(Rule::TryExceptInLoop) {
|
||||
perflint::rules::try_except_in_loop(checker, body);
|
||||
perflint::rules::try_except_in_loop(checker, &for_stmt.body);
|
||||
}
|
||||
if checker.is_rule_enabled(Rule::ForLoopSetMutations) {
|
||||
refurb::rules::for_loop_set_mutations(checker, for_stmt);
|
||||
@@ -1292,141 +1260,133 @@ pub(crate) fn statement(stmt: &Stmt, checker: &mut Checker) {
|
||||
}
|
||||
}
|
||||
if checker.is_rule_enabled(Rule::NeedlessElse) {
|
||||
ruff::rules::needless_else(checker, for_stmt.into());
|
||||
ruff::rules::needless_else(checker, (&**for_stmt).into());
|
||||
}
|
||||
}
|
||||
Stmt::Try(
|
||||
try_stmt @ ast::StmtTry {
|
||||
body,
|
||||
handlers,
|
||||
orelse,
|
||||
finalbody,
|
||||
..
|
||||
},
|
||||
) => {
|
||||
Stmt::Try(try_stmt) => {
|
||||
if checker.is_rule_enabled(Rule::TooManyNestedBlocks) {
|
||||
pylint::rules::too_many_nested_blocks(checker, stmt);
|
||||
}
|
||||
if checker.is_rule_enabled(Rule::JumpStatementInFinally) {
|
||||
flake8_bugbear::rules::jump_statement_in_finally(checker, finalbody);
|
||||
flake8_bugbear::rules::jump_statement_in_finally(checker, &try_stmt.finalbody);
|
||||
}
|
||||
if checker.is_rule_enabled(Rule::ContinueInFinally) {
|
||||
if checker.target_version() <= PythonVersion::PY38 {
|
||||
pylint::rules::continue_in_finally(checker, finalbody);
|
||||
pylint::rules::continue_in_finally(checker, &try_stmt.finalbody);
|
||||
}
|
||||
}
|
||||
if checker.is_rule_enabled(Rule::DefaultExceptNotLast) {
|
||||
pyflakes::rules::default_except_not_last(checker, handlers, checker.locator);
|
||||
pyflakes::rules::default_except_not_last(checker, &try_stmt.handlers, checker.locator);
|
||||
}
|
||||
if checker.any_rule_enabled(&[
|
||||
Rule::DuplicateHandlerException,
|
||||
Rule::DuplicateTryBlockException,
|
||||
]) {
|
||||
flake8_bugbear::rules::duplicate_exceptions(checker, handlers);
|
||||
flake8_bugbear::rules::duplicate_exceptions(checker, &try_stmt.handlers);
|
||||
}
|
||||
if checker.is_rule_enabled(Rule::RedundantTupleInExceptionHandler) {
|
||||
flake8_bugbear::rules::redundant_tuple_in_exception_handler(checker, handlers);
|
||||
flake8_bugbear::rules::redundant_tuple_in_exception_handler(checker, &try_stmt.handlers);
|
||||
}
|
||||
if checker.is_rule_enabled(Rule::OSErrorAlias) {
|
||||
pyupgrade::rules::os_error_alias_handlers(checker, handlers);
|
||||
pyupgrade::rules::os_error_alias_handlers(checker, &try_stmt.handlers);
|
||||
}
|
||||
if checker.is_rule_enabled(Rule::TimeoutErrorAlias) {
|
||||
if checker.target_version() >= PythonVersion::PY310 {
|
||||
pyupgrade::rules::timeout_error_alias_handlers(checker, handlers);
|
||||
pyupgrade::rules::timeout_error_alias_handlers(checker, &try_stmt.handlers);
|
||||
}
|
||||
}
|
||||
if checker.is_rule_enabled(Rule::PytestAssertInExcept) {
|
||||
flake8_pytest_style::rules::assert_in_exception_handler(checker, handlers);
|
||||
flake8_pytest_style::rules::assert_in_exception_handler(checker, &try_stmt.handlers);
|
||||
}
|
||||
if checker.is_rule_enabled(Rule::SuppressibleException) {
|
||||
flake8_simplify::rules::suppressible_exception(
|
||||
checker, stmt, body, handlers, orelse, finalbody,
|
||||
checker, stmt, &try_stmt.body, &try_stmt.handlers, &try_stmt.orelse, &try_stmt.finalbody,
|
||||
);
|
||||
}
|
||||
if checker.is_rule_enabled(Rule::ReturnInTryExceptFinally) {
|
||||
flake8_simplify::rules::return_in_try_except_finally(
|
||||
checker, body, handlers, finalbody,
|
||||
checker, &try_stmt.body, &try_stmt.handlers, &try_stmt.finalbody,
|
||||
);
|
||||
}
|
||||
if checker.is_rule_enabled(Rule::TryConsiderElse) {
|
||||
tryceratops::rules::try_consider_else(checker, body, orelse, handlers);
|
||||
tryceratops::rules::try_consider_else(checker, &try_stmt.body, &try_stmt.orelse, &try_stmt.handlers);
|
||||
}
|
||||
if checker.is_rule_enabled(Rule::VerboseRaise) {
|
||||
tryceratops::rules::verbose_raise(checker, handlers);
|
||||
tryceratops::rules::verbose_raise(checker, &try_stmt.handlers);
|
||||
}
|
||||
if checker.is_rule_enabled(Rule::VerboseLogMessage) {
|
||||
tryceratops::rules::verbose_log_message(checker, handlers);
|
||||
tryceratops::rules::verbose_log_message(checker, &try_stmt.handlers);
|
||||
}
|
||||
if checker.is_rule_enabled(Rule::RaiseWithinTry) {
|
||||
tryceratops::rules::raise_within_try(checker, body, handlers);
|
||||
tryceratops::rules::raise_within_try(checker, &try_stmt.body, &try_stmt.handlers);
|
||||
}
|
||||
if checker.is_rule_enabled(Rule::UselessTryExcept) {
|
||||
tryceratops::rules::useless_try_except(checker, handlers);
|
||||
tryceratops::rules::useless_try_except(checker, &try_stmt.handlers);
|
||||
}
|
||||
if checker.is_rule_enabled(Rule::ErrorInsteadOfException) {
|
||||
tryceratops::rules::error_instead_of_exception(checker, handlers);
|
||||
tryceratops::rules::error_instead_of_exception(checker, &try_stmt.handlers);
|
||||
}
|
||||
if checker.is_rule_enabled(Rule::NeedlessElse) {
|
||||
ruff::rules::needless_else(checker, try_stmt.into());
|
||||
ruff::rules::needless_else(checker, (&**try_stmt).into());
|
||||
}
|
||||
}
|
||||
Stmt::Assign(assign @ ast::StmtAssign { targets, value, .. }) => {
|
||||
Stmt::Assign(assign) => {
|
||||
if checker.is_rule_enabled(Rule::SelfOrClsAssignment) {
|
||||
for target in targets {
|
||||
for target in &assign.targets {
|
||||
pylint::rules::self_or_cls_assignment(checker, target);
|
||||
}
|
||||
}
|
||||
if checker.is_rule_enabled(Rule::RedeclaredAssignedName) {
|
||||
pylint::rules::redeclared_assigned_name(checker, targets);
|
||||
pylint::rules::redeclared_assigned_name(checker, &assign.targets);
|
||||
}
|
||||
if checker.is_rule_enabled(Rule::LambdaAssignment) {
|
||||
if let [target] = &targets[..] {
|
||||
pycodestyle::rules::lambda_assignment(checker, target, value, None, stmt);
|
||||
if let [target] = &assign.targets[..] {
|
||||
pycodestyle::rules::lambda_assignment(checker, target, &assign.value, None, stmt);
|
||||
}
|
||||
}
|
||||
if checker.is_rule_enabled(Rule::AssignmentToOsEnviron) {
|
||||
flake8_bugbear::rules::assignment_to_os_environ(checker, targets);
|
||||
flake8_bugbear::rules::assignment_to_os_environ(checker, &assign.targets);
|
||||
}
|
||||
if checker.is_rule_enabled(Rule::HardcodedPasswordString) {
|
||||
flake8_bandit::rules::assign_hardcoded_password_string(checker, value, targets);
|
||||
flake8_bandit::rules::assign_hardcoded_password_string(checker, &assign.value, &assign.targets);
|
||||
}
|
||||
if checker.is_rule_enabled(Rule::GlobalStatement) {
|
||||
for target in targets {
|
||||
if let Expr::Name(ast::ExprName { id, .. }) = target {
|
||||
pylint::rules::global_statement(checker, id);
|
||||
for target in &assign.targets {
|
||||
if let Expr::Name(name_expr) = target {
|
||||
pylint::rules::global_statement(checker, &name_expr.id);
|
||||
}
|
||||
}
|
||||
}
|
||||
if checker.is_rule_enabled(Rule::UselessMetaclassType) {
|
||||
pyupgrade::rules::useless_metaclass_type(checker, stmt, value, targets);
|
||||
pyupgrade::rules::useless_metaclass_type(checker, stmt, &assign.value, &assign.targets);
|
||||
}
|
||||
if checker.is_rule_enabled(Rule::ConvertTypedDictFunctionalToClass) {
|
||||
pyupgrade::rules::convert_typed_dict_functional_to_class(
|
||||
checker, stmt, targets, value,
|
||||
checker, stmt, &assign.targets, &assign.value,
|
||||
);
|
||||
}
|
||||
if checker.is_rule_enabled(Rule::ConvertNamedTupleFunctionalToClass) {
|
||||
pyupgrade::rules::convert_named_tuple_functional_to_class(
|
||||
checker, stmt, targets, value,
|
||||
checker, stmt, &assign.targets, &assign.value,
|
||||
);
|
||||
}
|
||||
if checker.is_rule_enabled(Rule::PandasDfVariableName) {
|
||||
pandas_vet::rules::assignment_to_df(checker, targets);
|
||||
pandas_vet::rules::assignment_to_df(checker, &assign.targets);
|
||||
}
|
||||
if checker.is_rule_enabled(Rule::AirflowVariableNameTaskIdMismatch) {
|
||||
airflow::rules::variable_name_task_id(checker, targets, value);
|
||||
airflow::rules::variable_name_task_id(checker, &assign.targets, &assign.value);
|
||||
}
|
||||
if checker.is_rule_enabled(Rule::SelfAssigningVariable) {
|
||||
pylint::rules::self_assignment(checker, assign);
|
||||
}
|
||||
if checker.is_rule_enabled(Rule::TypeParamNameMismatch) {
|
||||
pylint::rules::type_param_name_mismatch(checker, value, targets);
|
||||
pylint::rules::type_param_name_mismatch(checker, &assign.value, &assign.targets);
|
||||
}
|
||||
if checker.is_rule_enabled(Rule::TypeNameIncorrectVariance) {
|
||||
pylint::rules::type_name_incorrect_variance(checker, value);
|
||||
pylint::rules::type_name_incorrect_variance(checker, &assign.value);
|
||||
}
|
||||
if checker.is_rule_enabled(Rule::TypeBivariance) {
|
||||
pylint::rules::type_bivariance(checker, value);
|
||||
pylint::rules::type_bivariance(checker, &assign.value);
|
||||
}
|
||||
if checker.is_rule_enabled(Rule::NonAugmentedAssignment) {
|
||||
pylint::rules::non_augmented_assignment(checker, assign);
|
||||
@@ -1449,14 +1409,14 @@ pub(crate) fn statement(stmt: &Stmt, checker: &mut Checker) {
|
||||
.any(|scope| scope.kind.is_function())
|
||||
{
|
||||
if checker.is_rule_enabled(Rule::UnprefixedTypeParam) {
|
||||
flake8_pyi::rules::prefix_type_params(checker, value, targets);
|
||||
flake8_pyi::rules::prefix_type_params(checker, &assign.value, &assign.targets);
|
||||
}
|
||||
if checker.is_rule_enabled(Rule::AssignmentDefaultInStub) {
|
||||
flake8_pyi::rules::assignment_default_in_stub(checker, targets, value);
|
||||
flake8_pyi::rules::assignment_default_in_stub(checker, &assign.targets, &assign.value);
|
||||
}
|
||||
if checker.is_rule_enabled(Rule::UnannotatedAssignmentInStub) {
|
||||
flake8_pyi::rules::unannotated_assignment_in_stub(
|
||||
checker, targets, value,
|
||||
checker, &assign.targets, &assign.value,
|
||||
);
|
||||
}
|
||||
if checker.is_rule_enabled(Rule::ComplexAssignmentInStub) {
|
||||
@@ -1464,7 +1424,7 @@ pub(crate) fn statement(stmt: &Stmt, checker: &mut Checker) {
|
||||
}
|
||||
if checker.is_rule_enabled(Rule::TypeAliasWithoutAnnotation) {
|
||||
flake8_pyi::rules::type_alias_without_annotation(
|
||||
checker, value, targets,
|
||||
checker, &assign.value, &assign.targets,
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -1477,15 +1437,10 @@ pub(crate) fn statement(stmt: &Stmt, checker: &mut Checker) {
|
||||
pyupgrade::rules::non_pep695_type_alias_type(checker, assign);
|
||||
}
|
||||
}
|
||||
Stmt::AnnAssign(
|
||||
assign_stmt @ ast::StmtAnnAssign {
|
||||
target,
|
||||
value,
|
||||
annotation,
|
||||
..
|
||||
},
|
||||
) => {
|
||||
if let Some(value) = value {
|
||||
Stmt::AnnAssign(assign_stmt) => {
|
||||
let target = &assign_stmt.target;
|
||||
let annotation = &assign_stmt.annotation;
|
||||
if let Some(value) = &assign_stmt.value {
|
||||
if checker.is_rule_enabled(Rule::LambdaAssignment) {
|
||||
pycodestyle::rules::lambda_assignment(
|
||||
checker,
|
||||
@@ -1506,7 +1461,7 @@ pub(crate) fn statement(stmt: &Stmt, checker: &mut Checker) {
|
||||
flake8_bugbear::rules::unintentional_type_annotation(
|
||||
checker,
|
||||
target,
|
||||
value.as_deref(),
|
||||
assign_stmt.value.as_deref(),
|
||||
stmt,
|
||||
);
|
||||
}
|
||||
@@ -1514,7 +1469,7 @@ pub(crate) fn statement(stmt: &Stmt, checker: &mut Checker) {
|
||||
pyupgrade::rules::non_pep695_type_alias(checker, assign_stmt);
|
||||
}
|
||||
if checker.is_rule_enabled(Rule::HardcodedPasswordString) {
|
||||
if let Some(value) = value.as_deref() {
|
||||
if let Some(value) = assign_stmt.value.as_deref() {
|
||||
flake8_bandit::rules::assign_hardcoded_password_string(
|
||||
checker,
|
||||
value,
|
||||
@@ -1526,7 +1481,7 @@ pub(crate) fn statement(stmt: &Stmt, checker: &mut Checker) {
|
||||
ruff::rules::sort_dunder_all_ann_assign(checker, assign_stmt);
|
||||
}
|
||||
if checker.source_type.is_stub() {
|
||||
if let Some(value) = value {
|
||||
if let Some(value) = &assign_stmt.value {
|
||||
if checker.is_rule_enabled(Rule::AssignmentDefaultInStub) {
|
||||
// Ignore assignments in function bodies; those are covered by other rules.
|
||||
if !checker
|
||||
@@ -1563,7 +1518,8 @@ pub(crate) fn statement(stmt: &Stmt, checker: &mut Checker) {
|
||||
}
|
||||
}
|
||||
}
|
||||
Stmt::TypeAlias(ast::StmtTypeAlias { name, .. }) => {
|
||||
Stmt::TypeAlias(type_alias) => {
|
||||
let name = &type_alias.name;
|
||||
if checker.is_rule_enabled(Rule::SnakeCaseTypeAlias) {
|
||||
flake8_pyi::rules::snake_case_type_alias(checker, name);
|
||||
}
|
||||
@@ -1571,17 +1527,12 @@ pub(crate) fn statement(stmt: &Stmt, checker: &mut Checker) {
|
||||
flake8_pyi::rules::t_suffixed_type_alias(checker, name);
|
||||
}
|
||||
}
|
||||
Stmt::Delete(
|
||||
delete @ ast::StmtDelete {
|
||||
targets,
|
||||
range: _,
|
||||
node_index: _,
|
||||
},
|
||||
) => {
|
||||
Stmt::Delete(delete) => {
|
||||
let targets = &delete.targets;
|
||||
if checker.is_rule_enabled(Rule::GlobalStatement) {
|
||||
for target in targets {
|
||||
if let Expr::Name(ast::ExprName { id, .. }) = target {
|
||||
pylint::rules::global_statement(checker, id);
|
||||
if let Expr::Name(name_expr) = target {
|
||||
pylint::rules::global_statement(checker, &name_expr.id);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1618,12 +1569,13 @@ pub(crate) fn statement(stmt: &Stmt, checker: &mut Checker) {
|
||||
pylint::rules::useless_exception_statement(checker, expr);
|
||||
}
|
||||
}
|
||||
Stmt::Match(ast::StmtMatch {
|
||||
subject: _,
|
||||
cases,
|
||||
range: _,
|
||||
node_index: _,
|
||||
}) => {
|
||||
Stmt::Match(match_stmt) => {
|
||||
let ast::StmtMatch {
|
||||
subject: _,
|
||||
cases,
|
||||
range: _,
|
||||
node_index: _,
|
||||
} = &**match_stmt;
|
||||
if checker.is_rule_enabled(Rule::NanComparison) {
|
||||
pylint::rules::nan_comparison_match(checker, cases);
|
||||
}
|
||||
|
||||
@@ -782,7 +782,10 @@ impl SemanticSyntaxContext for Checker<'_> {
|
||||
for scope in self.semantic.current_scopes() {
|
||||
match scope.kind {
|
||||
ScopeKind::Class(_) | ScopeKind::Lambda(_) => return false,
|
||||
ScopeKind::Function(ast::StmtFunctionDef { is_async, .. }) => return *is_async,
|
||||
ScopeKind::Function(function_def) => {
|
||||
let is_async = &function_def.is_async;
|
||||
return *is_async;
|
||||
}
|
||||
ScopeKind::Generator { .. }
|
||||
| ScopeKind::Module
|
||||
| ScopeKind::Type
|
||||
@@ -870,9 +873,13 @@ impl SemanticSyntaxContext for Checker<'_> {
|
||||
|
||||
for parent in self.semantic.current_statements().skip(1) {
|
||||
match parent {
|
||||
Stmt::For(ast::StmtFor { orelse, .. })
|
||||
| Stmt::While(ast::StmtWhile { orelse, .. }) => {
|
||||
if !orelse.contains(child) {
|
||||
Stmt::For(node) => {
|
||||
if !node.orelse.contains(child) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
Stmt::While(node) => {
|
||||
if !node.orelse.contains(child) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
@@ -888,7 +895,8 @@ impl SemanticSyntaxContext for Checker<'_> {
|
||||
|
||||
fn is_bound_parameter(&self, name: &str) -> bool {
|
||||
match self.semantic.current_scope().kind {
|
||||
ScopeKind::Function(ast::StmtFunctionDef { parameters, .. }) => {
|
||||
ScopeKind::Function(function_def) => {
|
||||
let parameters = &function_def.parameters;
|
||||
parameters.includes(name)
|
||||
}
|
||||
ScopeKind::Class(_)
|
||||
@@ -932,12 +940,13 @@ impl<'a> Visitor<'a> for Checker<'a> {
|
||||
{
|
||||
self.semantic.flags |= SemanticModelFlags::MODULE_DOCSTRING_BOUNDARY;
|
||||
}
|
||||
Stmt::ImportFrom(ast::StmtImportFrom { module, names, .. }) => {
|
||||
Stmt::ImportFrom(node) => {
|
||||
self.semantic.flags |= SemanticModelFlags::MODULE_DOCSTRING_BOUNDARY;
|
||||
|
||||
// Allow __future__ imports until we see a non-__future__ import.
|
||||
if let Some("__future__") = module.as_deref() {
|
||||
if names
|
||||
if let Some("__future__") = node.module.as_deref() {
|
||||
if node
|
||||
.names
|
||||
.iter()
|
||||
.any(|alias| alias.name.as_str() == "annotations")
|
||||
{
|
||||
@@ -981,20 +990,22 @@ impl<'a> Visitor<'a> for Checker<'a> {
|
||||
|
||||
// Step 1: Binding
|
||||
match stmt {
|
||||
Stmt::AugAssign(ast::StmtAugAssign {
|
||||
target,
|
||||
op: _,
|
||||
value: _,
|
||||
range: _,
|
||||
node_index: _,
|
||||
}) => {
|
||||
Stmt::AugAssign(node) => {
|
||||
let ast::StmtAugAssign {
|
||||
target,
|
||||
op: _,
|
||||
value: _,
|
||||
range: _,
|
||||
node_index: _,
|
||||
} = &**node;
|
||||
self.handle_node_load(target);
|
||||
}
|
||||
Stmt::Import(ast::StmtImport {
|
||||
names,
|
||||
range: _,
|
||||
node_index: _,
|
||||
}) => {
|
||||
Stmt::Import(node) => {
|
||||
let ast::StmtImport {
|
||||
names,
|
||||
range: _,
|
||||
node_index: _,
|
||||
} = &**node;
|
||||
if self.semantic.at_top_level() {
|
||||
self.importer.visit_import(stmt);
|
||||
}
|
||||
@@ -1043,13 +1054,14 @@ impl<'a> Visitor<'a> for Checker<'a> {
|
||||
}
|
||||
}
|
||||
}
|
||||
Stmt::ImportFrom(ast::StmtImportFrom {
|
||||
names,
|
||||
module,
|
||||
level,
|
||||
range: _,
|
||||
node_index: _,
|
||||
}) => {
|
||||
Stmt::ImportFrom(node) => {
|
||||
let ast::StmtImportFrom {
|
||||
names,
|
||||
module,
|
||||
level,
|
||||
range: _,
|
||||
node_index: _,
|
||||
} = &**node;
|
||||
if self.semantic.at_top_level() {
|
||||
self.importer.visit_import(stmt);
|
||||
}
|
||||
@@ -1110,11 +1122,12 @@ impl<'a> Visitor<'a> for Checker<'a> {
|
||||
}
|
||||
}
|
||||
}
|
||||
Stmt::Global(ast::StmtGlobal {
|
||||
names,
|
||||
range: _,
|
||||
node_index: _,
|
||||
}) => {
|
||||
Stmt::Global(node) => {
|
||||
let ast::StmtGlobal {
|
||||
names,
|
||||
range: _,
|
||||
node_index: _,
|
||||
} = &**node;
|
||||
if !self.semantic.scope_id.is_global() {
|
||||
for name in names {
|
||||
let binding_id = self.semantic.global_scope().get(name);
|
||||
@@ -1136,11 +1149,12 @@ impl<'a> Visitor<'a> for Checker<'a> {
|
||||
}
|
||||
}
|
||||
}
|
||||
Stmt::Nonlocal(ast::StmtNonlocal {
|
||||
names,
|
||||
range: _,
|
||||
node_index: _,
|
||||
}) => {
|
||||
Stmt::Nonlocal(node) => {
|
||||
let ast::StmtNonlocal {
|
||||
names,
|
||||
range: _,
|
||||
node_index: _,
|
||||
} = &**node;
|
||||
if !self.semantic.scope_id.is_global() {
|
||||
for name in names {
|
||||
if let Some((scope_id, binding_id)) = self.semantic.nonlocal(name) {
|
||||
@@ -1174,17 +1188,13 @@ impl<'a> Visitor<'a> for Checker<'a> {
|
||||
|
||||
// Step 2: Traversal
|
||||
match stmt {
|
||||
Stmt::FunctionDef(
|
||||
function_def @ ast::StmtFunctionDef {
|
||||
name,
|
||||
body,
|
||||
parameters,
|
||||
decorator_list,
|
||||
returns,
|
||||
type_params,
|
||||
..
|
||||
},
|
||||
) => {
|
||||
Stmt::FunctionDef(function_def) => {
|
||||
let name = &function_def.name;
|
||||
let body = &function_def.body;
|
||||
let parameters = &function_def.parameters;
|
||||
let decorator_list = &function_def.decorator_list;
|
||||
let returns = &function_def.returns;
|
||||
let type_params = &function_def.type_params;
|
||||
// Visit the decorators and arguments, but avoid the body, which will be
|
||||
// deferred.
|
||||
for decorator in decorator_list {
|
||||
@@ -1313,16 +1323,12 @@ impl<'a> Visitor<'a> for Checker<'a> {
|
||||
BindingFlags::empty(),
|
||||
);
|
||||
}
|
||||
Stmt::ClassDef(
|
||||
class_def @ ast::StmtClassDef {
|
||||
name,
|
||||
body,
|
||||
arguments,
|
||||
decorator_list,
|
||||
type_params,
|
||||
..
|
||||
},
|
||||
) => {
|
||||
Stmt::ClassDef(class_def) => {
|
||||
let name = &class_def.name;
|
||||
let body = &class_def.body;
|
||||
let arguments = &class_def.arguments;
|
||||
let decorator_list = &class_def.decorator_list;
|
||||
let type_params = &class_def.type_params;
|
||||
for decorator in decorator_list {
|
||||
self.visit_decorator(decorator);
|
||||
}
|
||||
@@ -1369,30 +1375,20 @@ impl<'a> Visitor<'a> for Checker<'a> {
|
||||
BindingFlags::empty(),
|
||||
);
|
||||
}
|
||||
Stmt::TypeAlias(ast::StmtTypeAlias {
|
||||
range: _,
|
||||
node_index: _,
|
||||
name,
|
||||
type_params,
|
||||
value,
|
||||
}) => {
|
||||
Stmt::TypeAlias(node) => {
|
||||
self.semantic.push_scope(ScopeKind::Type);
|
||||
if let Some(type_params) = type_params {
|
||||
if let Some(type_params) = &node.type_params {
|
||||
self.visit_type_params(type_params);
|
||||
}
|
||||
self.visit_deferred_type_alias_value(value);
|
||||
self.visit_deferred_type_alias_value(&node.value);
|
||||
self.semantic.pop_scope();
|
||||
self.visit_expr(name);
|
||||
self.visit_expr(&node.name);
|
||||
}
|
||||
Stmt::Try(
|
||||
try_node @ ast::StmtTry {
|
||||
body,
|
||||
handlers,
|
||||
orelse,
|
||||
finalbody,
|
||||
..
|
||||
},
|
||||
) => {
|
||||
Stmt::Try(try_node) => {
|
||||
let body = &try_node.body;
|
||||
let handlers = &try_node.handlers;
|
||||
let orelse = &try_node.orelse;
|
||||
let finalbody = &try_node.finalbody;
|
||||
// Iterate over the `body`, then the `handlers`, then the `orelse`, then the
|
||||
// `finalbody`, but treat the body and the `orelse` as a single branch for
|
||||
// flow analysis purposes.
|
||||
@@ -1418,64 +1414,60 @@ impl<'a> Visitor<'a> for Checker<'a> {
|
||||
self.visit_body(finalbody);
|
||||
self.semantic.pop_branch();
|
||||
}
|
||||
Stmt::AnnAssign(ast::StmtAnnAssign {
|
||||
target,
|
||||
annotation,
|
||||
value,
|
||||
..
|
||||
}) => {
|
||||
Stmt::AnnAssign(node) => {
|
||||
match AnnotationContext::from_model(
|
||||
&self.semantic,
|
||||
self.settings(),
|
||||
self.target_version(),
|
||||
) {
|
||||
AnnotationContext::RuntimeRequired => {
|
||||
self.visit_runtime_required_annotation(annotation);
|
||||
self.visit_runtime_required_annotation(&node.annotation);
|
||||
}
|
||||
AnnotationContext::RuntimeEvaluated
|
||||
if flake8_type_checking::helpers::is_dataclass_meta_annotation(
|
||||
annotation,
|
||||
&node.annotation,
|
||||
self.semantic(),
|
||||
) =>
|
||||
{
|
||||
self.visit_runtime_required_annotation(annotation);
|
||||
self.visit_runtime_required_annotation(&node.annotation);
|
||||
}
|
||||
AnnotationContext::RuntimeEvaluated => {
|
||||
self.visit_runtime_evaluated_annotation(annotation);
|
||||
self.visit_runtime_evaluated_annotation(&node.annotation);
|
||||
}
|
||||
AnnotationContext::TypingOnly
|
||||
if flake8_type_checking::helpers::is_dataclass_meta_annotation(
|
||||
annotation,
|
||||
&node.annotation,
|
||||
self.semantic(),
|
||||
) =>
|
||||
{
|
||||
if let Expr::Subscript(subscript) = &**annotation {
|
||||
if let Expr::Subscript(subscript) = &*node.annotation {
|
||||
// Ex) `InitVar[str]`
|
||||
self.visit_runtime_required_annotation(&subscript.value);
|
||||
self.visit_annotation(&subscript.slice);
|
||||
} else {
|
||||
// Ex) `InitVar`
|
||||
self.visit_runtime_required_annotation(annotation);
|
||||
self.visit_runtime_required_annotation(&node.annotation);
|
||||
}
|
||||
}
|
||||
AnnotationContext::TypingOnly => self.visit_annotation(annotation),
|
||||
AnnotationContext::TypingOnly => self.visit_annotation(&node.annotation),
|
||||
}
|
||||
|
||||
if let Some(expr) = value {
|
||||
if self.semantic.match_typing_expr(annotation, "TypeAlias") {
|
||||
if let Some(expr) = &node.value {
|
||||
if self.semantic.match_typing_expr(&node.annotation, "TypeAlias") {
|
||||
self.visit_annotated_type_alias_value(expr);
|
||||
} else {
|
||||
self.visit_expr(expr);
|
||||
}
|
||||
}
|
||||
self.visit_expr(target);
|
||||
self.visit_expr(&node.target);
|
||||
}
|
||||
Stmt::Assert(ast::StmtAssert {
|
||||
test,
|
||||
msg,
|
||||
range: _,
|
||||
node_index: _,
|
||||
}) => {
|
||||
Stmt::Assert(node) => {
|
||||
let ast::StmtAssert {
|
||||
test,
|
||||
msg,
|
||||
range: _,
|
||||
node_index: _,
|
||||
} = &**node;
|
||||
let snapshot = self.semantic.flags;
|
||||
self.semantic.flags |= SemanticModelFlags::ASSERT_STATEMENT;
|
||||
self.visit_boolean_test(test);
|
||||
@@ -1484,13 +1476,14 @@ impl<'a> Visitor<'a> for Checker<'a> {
|
||||
}
|
||||
self.semantic.flags = snapshot;
|
||||
}
|
||||
Stmt::With(ast::StmtWith {
|
||||
items,
|
||||
body,
|
||||
is_async: _,
|
||||
range: _,
|
||||
node_index: _,
|
||||
}) => {
|
||||
Stmt::With(node) => {
|
||||
let ast::StmtWith {
|
||||
items,
|
||||
body,
|
||||
is_async: _,
|
||||
range: _,
|
||||
node_index: _,
|
||||
} = &**node;
|
||||
for item in items {
|
||||
self.visit_with_item(item);
|
||||
}
|
||||
@@ -1498,26 +1491,22 @@ impl<'a> Visitor<'a> for Checker<'a> {
|
||||
self.visit_body(body);
|
||||
self.semantic.pop_branch();
|
||||
}
|
||||
Stmt::While(ast::StmtWhile {
|
||||
test,
|
||||
body,
|
||||
orelse,
|
||||
range: _,
|
||||
node_index: _,
|
||||
}) => {
|
||||
Stmt::While(node) => {
|
||||
let ast::StmtWhile {
|
||||
test,
|
||||
body,
|
||||
orelse,
|
||||
range: _,
|
||||
node_index: _,
|
||||
} = &**node;
|
||||
self.visit_boolean_test(test);
|
||||
self.visit_body(body);
|
||||
self.visit_body(orelse);
|
||||
}
|
||||
Stmt::If(
|
||||
stmt_if @ ast::StmtIf {
|
||||
test,
|
||||
body,
|
||||
elif_else_clauses,
|
||||
range: _,
|
||||
node_index: _,
|
||||
},
|
||||
) => {
|
||||
Stmt::If(stmt_if) => {
|
||||
let test = &stmt_if.test;
|
||||
let body = &stmt_if.body;
|
||||
let elif_else_clauses = &stmt_if.elif_else_clauses;
|
||||
self.visit_boolean_test(test);
|
||||
|
||||
self.semantic.push_branch();
|
||||
@@ -1542,14 +1531,14 @@ impl<'a> Visitor<'a> for Checker<'a> {
|
||||
|
||||
if self.semantic().at_top_level() || self.semantic().current_scope().kind.is_class() {
|
||||
match stmt {
|
||||
Stmt::Assign(ast::StmtAssign { targets, .. }) => {
|
||||
if let [Expr::Name(_)] = targets.as_slice() {
|
||||
Stmt::Assign(node) => {
|
||||
if let [Expr::Name(_)] = node.targets.as_slice() {
|
||||
self.docstring_state =
|
||||
DocstringState::Expected(ExpectedDocstringKind::Attribute);
|
||||
}
|
||||
}
|
||||
Stmt::AnnAssign(ast::StmtAnnAssign { target, .. }) => {
|
||||
if target.is_name_expr() {
|
||||
Stmt::AnnAssign(node) => {
|
||||
if node.target.is_name_expr() {
|
||||
self.docstring_state =
|
||||
DocstringState::Expected(ExpectedDocstringKind::Attribute);
|
||||
}
|
||||
@@ -2690,13 +2679,13 @@ impl<'a> Checker<'a> {
|
||||
|
||||
match parent {
|
||||
Stmt::TypeAlias(_) => flags.insert(BindingFlags::DEFERRED_TYPE_ALIAS),
|
||||
Stmt::AnnAssign(ast::StmtAnnAssign { annotation, .. }) => {
|
||||
Stmt::AnnAssign(node) => {
|
||||
// TODO: It is a bit unfortunate that we do this check twice
|
||||
// maybe we should change how we visit this statement
|
||||
// so the semantic flag for the type alias sticks around
|
||||
// until after we've handled this store, so we can check
|
||||
// the flag instead of duplicating this check
|
||||
if self.semantic.match_typing_expr(annotation, "TypeAlias") {
|
||||
if self.semantic.match_typing_expr(&node.annotation, "TypeAlias") {
|
||||
flags.insert(BindingFlags::ANNOTATED_TYPE_ALIAS);
|
||||
}
|
||||
}
|
||||
@@ -2707,22 +2696,22 @@ impl<'a> Checker<'a> {
|
||||
|
||||
if scope.kind.is_module()
|
||||
&& match parent {
|
||||
Stmt::Assign(ast::StmtAssign { targets, .. }) => {
|
||||
if let Some(Expr::Name(ast::ExprName { id, .. })) = targets.first() {
|
||||
Stmt::Assign(node) => {
|
||||
if let Some(Expr::Name(ast::ExprName { id, .. })) = node.targets.first() {
|
||||
id == "__all__"
|
||||
} else {
|
||||
false
|
||||
}
|
||||
}
|
||||
Stmt::AugAssign(ast::StmtAugAssign { target, .. }) => {
|
||||
if let Expr::Name(ast::ExprName { id, .. }) = target.as_ref() {
|
||||
Stmt::AugAssign(node) => {
|
||||
if let Expr::Name(ast::ExprName { id, .. }) = node.target.as_ref() {
|
||||
id == "__all__"
|
||||
} else {
|
||||
false
|
||||
}
|
||||
}
|
||||
Stmt::AnnAssign(ast::StmtAnnAssign { target, .. }) => {
|
||||
if let Expr::Name(ast::ExprName { id, .. }) = target.as_ref() {
|
||||
Stmt::AnnAssign(node) => {
|
||||
if let Expr::Name(ast::ExprName { id, .. }) = node.target.as_ref() {
|
||||
id == "__all__"
|
||||
} else {
|
||||
false
|
||||
@@ -2765,10 +2754,8 @@ impl<'a> Checker<'a> {
|
||||
// Match the left-hand side of an annotated assignment without a value,
|
||||
// like `x` in `x: int`. N.B. In stub files, these should be viewed
|
||||
// as assignments on par with statements such as `x: int = 5`.
|
||||
if matches!(
|
||||
parent,
|
||||
Stmt::AnnAssign(ast::StmtAnnAssign { value: None, .. })
|
||||
) && !self.semantic.in_annotation()
|
||||
if matches!(parent, Stmt::AnnAssign(node) if node.value.is_none())
|
||||
&& !self.semantic.in_annotation()
|
||||
{
|
||||
self.add_binding(id, expr.range(), BindingKind::Annotation, flags);
|
||||
return;
|
||||
@@ -3040,19 +3027,16 @@ impl<'a> Checker<'a> {
|
||||
|
||||
let stmt = self.semantic.current_statement();
|
||||
|
||||
let Stmt::FunctionDef(ast::StmtFunctionDef {
|
||||
body, parameters, ..
|
||||
}) = stmt
|
||||
else {
|
||||
let Stmt::FunctionDef(node) = stmt else {
|
||||
unreachable!("Expected Stmt::FunctionDef")
|
||||
};
|
||||
|
||||
self.with_semantic_checker(|semantic, context| semantic.visit_stmt(stmt, context));
|
||||
|
||||
self.visit_parameters(parameters);
|
||||
self.visit_parameters(&node.parameters);
|
||||
// Set the docstring state before visiting the function body.
|
||||
self.docstring_state = DocstringState::Expected(ExpectedDocstringKind::Function);
|
||||
self.visit_body(body);
|
||||
self.visit_body(&node.body);
|
||||
}
|
||||
}
|
||||
self.semantic.restore(snapshot);
|
||||
|
||||
@@ -127,8 +127,8 @@ pub(crate) fn make_redundant_alias<'a>(
|
||||
stmt: &Stmt,
|
||||
) -> Vec<Edit> {
|
||||
let aliases = match stmt {
|
||||
Stmt::Import(ast::StmtImport { names, .. }) => names,
|
||||
Stmt::ImportFrom(ast::StmtImportFrom { names, .. }) => names,
|
||||
Stmt::Import(node) => &node.names,
|
||||
Stmt::ImportFrom(node) => &node.names,
|
||||
_ => {
|
||||
return Vec::new();
|
||||
}
|
||||
@@ -286,7 +286,12 @@ pub(crate) fn add_argument(argument: &str, arguments: &Arguments, tokens: &Token
|
||||
|
||||
/// Generic function to add a (regular) parameter to a function definition.
|
||||
pub(crate) fn add_parameter(parameter: &str, parameters: &Parameters, source: &str) -> Edit {
|
||||
if let Some(last) = parameters.args.iter().rfind(|arg| arg.default.is_none()) {
|
||||
if let Some(last) = parameters
|
||||
.args
|
||||
.iter()
|
||||
.filter(|arg| arg.default.is_none())
|
||||
.next_back()
|
||||
{
|
||||
// Case 1: at least one regular parameter, so append after the last one.
|
||||
Edit::insertion(format!(", {parameter}"), last.end())
|
||||
} else if !parameters.args.is_empty() {
|
||||
@@ -399,43 +404,46 @@ fn is_only<T: PartialEq>(vec: &[T], value: &T) -> bool {
|
||||
/// Determine if a child is the only statement in its body.
|
||||
fn is_lone_child(child: &Stmt, parent: &Stmt) -> bool {
|
||||
match parent {
|
||||
Stmt::FunctionDef(ast::StmtFunctionDef { body, .. })
|
||||
| Stmt::ClassDef(ast::StmtClassDef { body, .. })
|
||||
| Stmt::With(ast::StmtWith { body, .. }) => {
|
||||
if is_only(body, child) {
|
||||
Stmt::FunctionDef(node) => {
|
||||
if is_only(&node.body, child) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
Stmt::For(ast::StmtFor { body, orelse, .. })
|
||||
| Stmt::While(ast::StmtWhile { body, orelse, .. }) => {
|
||||
if is_only(body, child) || is_only(orelse, child) {
|
||||
Stmt::ClassDef(node) => {
|
||||
if is_only(&node.body, child) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
Stmt::If(ast::StmtIf {
|
||||
body,
|
||||
elif_else_clauses,
|
||||
..
|
||||
}) => {
|
||||
if is_only(body, child)
|
||||
|| elif_else_clauses
|
||||
Stmt::With(node) => {
|
||||
if is_only(&node.body, child) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
Stmt::For(node) => {
|
||||
if is_only(&node.body, child) || is_only(&node.orelse, child) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
Stmt::While(node) => {
|
||||
if is_only(&node.body, child) || is_only(&node.orelse, child) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
Stmt::If(node) => {
|
||||
if is_only(&node.body, child)
|
||||
|| node
|
||||
.elif_else_clauses
|
||||
.iter()
|
||||
.any(|ast::ElifElseClause { body, .. }| is_only(body, child))
|
||||
{
|
||||
return true;
|
||||
}
|
||||
}
|
||||
Stmt::Try(ast::StmtTry {
|
||||
body,
|
||||
handlers,
|
||||
orelse,
|
||||
finalbody,
|
||||
..
|
||||
}) => {
|
||||
if is_only(body, child)
|
||||
|| is_only(orelse, child)
|
||||
|| is_only(finalbody, child)
|
||||
|| handlers.iter().any(|handler| match handler {
|
||||
Stmt::Try(node) => {
|
||||
if is_only(&node.body, child)
|
||||
|| is_only(&node.orelse, child)
|
||||
|| is_only(&node.finalbody, child)
|
||||
|| node.handlers.iter().any(|handler| match handler {
|
||||
ExceptHandler::ExceptHandler(ast::ExceptHandlerExceptHandler {
|
||||
body, ..
|
||||
}) => is_only(body, child),
|
||||
@@ -444,8 +452,8 @@ fn is_lone_child(child: &Stmt, parent: &Stmt) -> bool {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
Stmt::Match(ast::StmtMatch { cases, .. }) => {
|
||||
if cases.iter().any(|case| is_only(&case.body, child)) {
|
||||
Stmt::Match(node) => {
|
||||
if node.cases.iter().any(|case| is_only(&case.body, child)) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -236,9 +236,10 @@ impl<'a> Importer<'a> {
|
||||
semantic: &SemanticModel<'a>,
|
||||
type_checking_block: &Stmt,
|
||||
) -> Option<&'a Stmt> {
|
||||
let Stmt::If(ast::StmtIf { test, .. }) = type_checking_block else {
|
||||
let Stmt::If(node) = type_checking_block else {
|
||||
return None;
|
||||
};
|
||||
let test = &node.test;
|
||||
|
||||
let mut source = test;
|
||||
while let Expr::Attribute(ast::ExprAttribute { value, .. }) = source.as_ref() {
|
||||
@@ -453,17 +454,10 @@ impl<'a> Importer<'a> {
|
||||
if stmt.start() >= at {
|
||||
break;
|
||||
}
|
||||
if let Stmt::ImportFrom(ast::StmtImportFrom {
|
||||
module: name,
|
||||
names,
|
||||
level,
|
||||
range: _,
|
||||
node_index: _,
|
||||
}) = stmt
|
||||
{
|
||||
if *level == 0
|
||||
&& name.as_ref().is_some_and(|name| name == module)
|
||||
&& names.iter().all(|alias| alias.name.as_str() != "*")
|
||||
if let Stmt::ImportFrom(node) = stmt {
|
||||
if node.level == 0
|
||||
&& node.module.as_ref().is_some_and(|name| name == module)
|
||||
&& node.names.iter().all(|alias| alias.name.as_str() != "*")
|
||||
{
|
||||
import_from = Some(*stmt);
|
||||
}
|
||||
|
||||
@@ -1001,7 +1001,6 @@ mod tests {
|
||||
#[test_case(Path::new("write_to_debug.py"), PythonVersion::PY310)]
|
||||
#[test_case(Path::new("invalid_expression.py"), PythonVersion::PY312)]
|
||||
#[test_case(Path::new("global_parameter.py"), PythonVersion::PY310)]
|
||||
#[test_case(Path::new("annotated_global.py"), PythonVersion::PY314)]
|
||||
fn test_semantic_errors(path: &Path, python_version: PythonVersion) -> Result<()> {
|
||||
let snapshot = format!(
|
||||
"semantic_syntax_error_{}_{}",
|
||||
|
||||
@@ -281,12 +281,10 @@ impl Renamer {
|
||||
) -> Option<Edit> {
|
||||
let statement = binding.statement(semantic)?;
|
||||
|
||||
let (ast::Stmt::Assign(ast::StmtAssign { value, .. })
|
||||
| ast::Stmt::AnnAssign(ast::StmtAnnAssign {
|
||||
value: Some(value), ..
|
||||
})) = statement
|
||||
else {
|
||||
return None;
|
||||
let value = match statement {
|
||||
ast::Stmt::Assign(node) => &node.value,
|
||||
ast::Stmt::AnnAssign(node) => node.value.as_ref()?,
|
||||
_ => return None,
|
||||
};
|
||||
|
||||
let ast::ExprCall {
|
||||
|
||||
@@ -448,11 +448,10 @@ fn is_kwarg_parameter(semantic: &SemanticModel, name: &ExprName) -> bool {
|
||||
return false;
|
||||
};
|
||||
let binding = semantic.binding(binding_id);
|
||||
let Some(Stmt::FunctionDef(StmtFunctionDef { parameters, .. })) = binding.statement(semantic)
|
||||
else {
|
||||
let Some(Stmt::FunctionDef(node)) = binding.statement(semantic) else {
|
||||
return false;
|
||||
};
|
||||
parameters
|
||||
node.parameters
|
||||
.kwarg
|
||||
.as_deref()
|
||||
.is_some_and(|kwarg| kwarg.name.as_str() == name.id.as_str())
|
||||
|
||||
@@ -2,7 +2,7 @@
|
||||
//!
|
||||
//! See: <https://bandit.readthedocs.io/en/latest/blacklists/blacklist_imports.html>
|
||||
use ruff_macros::{ViolationMetadata, derive_message_formats};
|
||||
use ruff_python_ast::{self as ast, Stmt};
|
||||
use ruff_python_ast::Stmt;
|
||||
use ruff_text_size::Ranged;
|
||||
|
||||
use crate::Violation;
|
||||
@@ -371,7 +371,8 @@ pub(crate) fn suspicious_imports(checker: &Checker, stmt: &Stmt) {
|
||||
}
|
||||
|
||||
match stmt {
|
||||
Stmt::Import(ast::StmtImport { names, .. }) => {
|
||||
Stmt::Import(node) => {
|
||||
let names = &node.names;
|
||||
for name in names {
|
||||
match name.name.as_str() {
|
||||
"telnetlib" => {
|
||||
@@ -421,8 +422,9 @@ pub(crate) fn suspicious_imports(checker: &Checker, stmt: &Stmt) {
|
||||
}
|
||||
}
|
||||
}
|
||||
Stmt::ImportFrom(ast::StmtImportFrom { module, names, .. }) => {
|
||||
let Some(identifier) = module else { return };
|
||||
Stmt::ImportFrom(node) => {
|
||||
let Some(identifier) = &node.module else { return };
|
||||
let names = &node.names;
|
||||
match identifier.as_str() {
|
||||
"telnetlib" => {
|
||||
checker.report_diagnostic_if_enabled(
|
||||
|
||||
@@ -154,10 +154,12 @@ impl<'a> StatementVisitor<'a> for ReraiseVisitor<'a> {
|
||||
return;
|
||||
}
|
||||
match stmt {
|
||||
Stmt::Raise(ast::StmtRaise { exc, cause, .. }) => {
|
||||
Stmt::Raise(node) => {
|
||||
let exc = node.exc.as_deref();
|
||||
let cause = node.cause.as_deref();
|
||||
// except Exception [as <name>]:
|
||||
// raise [<exc> [from <cause>]]
|
||||
let reraised = match (self.name, exc.as_deref(), cause.as_deref()) {
|
||||
let reraised = match (self.name, exc, cause) {
|
||||
// `raise`
|
||||
(_, None, None) => true,
|
||||
// `raise SomeExc from <name>`
|
||||
|
||||
@@ -173,24 +173,21 @@ pub(crate) fn abstract_base_class(
|
||||
// If an ABC declares an attribute by providing a type annotation
|
||||
// but does not actually assign a value for that attribute,
|
||||
// assume it is intended to be an "abstract attribute"
|
||||
if matches!(
|
||||
stmt,
|
||||
Stmt::AnnAssign(ast::StmtAnnAssign { value: None, .. })
|
||||
) {
|
||||
has_abstract_method = true;
|
||||
continue;
|
||||
if let Stmt::AnnAssign(node) = stmt {
|
||||
if node.value.is_none() {
|
||||
has_abstract_method = true;
|
||||
continue;
|
||||
}
|
||||
}
|
||||
|
||||
let Stmt::FunctionDef(ast::StmtFunctionDef {
|
||||
decorator_list,
|
||||
body,
|
||||
name: method_name,
|
||||
..
|
||||
}) = stmt
|
||||
else {
|
||||
let Stmt::FunctionDef(node) = stmt else {
|
||||
continue;
|
||||
};
|
||||
|
||||
let decorator_list = &node.decorator_list;
|
||||
let body = &node.body;
|
||||
let method_name = &node.name;
|
||||
|
||||
let has_abstract_decorator = is_abstract(decorator_list, checker.semantic());
|
||||
has_abstract_method |= has_abstract_decorator;
|
||||
|
||||
|
||||
@@ -51,7 +51,7 @@ impl AlwaysFixableViolation for AssertFalse {
|
||||
}
|
||||
|
||||
fn assertion_error(msg: Option<&Expr>) -> Stmt {
|
||||
Stmt::Raise(ast::StmtRaise {
|
||||
Stmt::Raise(Box::new(ast::StmtRaise {
|
||||
range: TextRange::default(),
|
||||
node_index: ruff_python_ast::AtomicNodeIndex::NONE,
|
||||
exc: Some(Box::new(Expr::Call(ast::ExprCall {
|
||||
@@ -75,7 +75,7 @@ fn assertion_error(msg: Option<&Expr>) -> Stmt {
|
||||
node_index: ruff_python_ast::AtomicNodeIndex::NONE,
|
||||
}))),
|
||||
cause: None,
|
||||
})
|
||||
}))
|
||||
}
|
||||
|
||||
/// B011
|
||||
|
||||
@@ -114,14 +114,14 @@ pub(crate) fn class_as_data_structure(checker: &Checker, class_def: &ast::StmtCl
|
||||
// assignment of a name to an attribute.
|
||||
fn is_simple_assignment_to_attribute(stmt: &ast::Stmt) -> bool {
|
||||
match stmt {
|
||||
ast::Stmt::Assign(ast::StmtAssign { targets, value, .. }) => {
|
||||
let [target] = targets.as_slice() else {
|
||||
ast::Stmt::Assign(node) => {
|
||||
let [target] = node.targets.as_slice() else {
|
||||
return false;
|
||||
};
|
||||
target.is_attribute_expr() && value.is_name_expr()
|
||||
target.is_attribute_expr() && node.value.is_name_expr()
|
||||
}
|
||||
ast::Stmt::AnnAssign(ast::StmtAnnAssign { target, value, .. }) => {
|
||||
target.is_attribute_expr() && value.as_ref().is_some_and(|val| val.is_name_expr())
|
||||
ast::Stmt::AnnAssign(node) => {
|
||||
node.target.is_attribute_expr() && node.value.as_ref().is_some_and(|val| val.is_name_expr())
|
||||
}
|
||||
_ => false,
|
||||
}
|
||||
|
||||
@@ -86,12 +86,10 @@ struct SuspiciousVariablesVisitor<'a> {
|
||||
impl<'a> Visitor<'a> for SuspiciousVariablesVisitor<'a> {
|
||||
fn visit_stmt(&mut self, stmt: &'a Stmt) {
|
||||
match stmt {
|
||||
Stmt::FunctionDef(ast::StmtFunctionDef {
|
||||
parameters, body, ..
|
||||
}) => {
|
||||
Stmt::FunctionDef(node) => {
|
||||
// Collect all loaded variable names.
|
||||
let mut visitor = LoadedNamesVisitor::default();
|
||||
visitor.visit_body(body);
|
||||
visitor.visit_body(&node.body);
|
||||
|
||||
// Treat any non-arguments as "suspicious".
|
||||
self.names
|
||||
@@ -100,7 +98,7 @@ impl<'a> Visitor<'a> for SuspiciousVariablesVisitor<'a> {
|
||||
return false;
|
||||
}
|
||||
|
||||
if parameters.includes(&loaded.id) {
|
||||
if node.parameters.includes(&loaded.id) {
|
||||
return false;
|
||||
}
|
||||
|
||||
@@ -242,18 +240,26 @@ impl<'a> Visitor<'a> for AssignedNamesVisitor<'a> {
|
||||
}
|
||||
|
||||
match stmt {
|
||||
Stmt::Assign(ast::StmtAssign { targets, .. }) => {
|
||||
Stmt::Assign(node) => {
|
||||
let mut visitor = NamesFromAssignmentsVisitor::default();
|
||||
for expr in targets {
|
||||
for expr in &node.targets {
|
||||
visitor.visit_expr(expr);
|
||||
}
|
||||
self.names.extend(visitor.names);
|
||||
}
|
||||
Stmt::AugAssign(ast::StmtAugAssign { target, .. })
|
||||
| Stmt::AnnAssign(ast::StmtAnnAssign { target, .. })
|
||||
| Stmt::For(ast::StmtFor { target, .. }) => {
|
||||
Stmt::AugAssign(node) => {
|
||||
let mut visitor = NamesFromAssignmentsVisitor::default();
|
||||
visitor.visit_expr(target);
|
||||
visitor.visit_expr(&node.target);
|
||||
self.names.extend(visitor.names);
|
||||
}
|
||||
Stmt::AnnAssign(node) => {
|
||||
let mut visitor = NamesFromAssignmentsVisitor::default();
|
||||
visitor.visit_expr(&node.target);
|
||||
self.names.extend(visitor.names);
|
||||
}
|
||||
Stmt::For(node) => {
|
||||
let mut visitor = NamesFromAssignmentsVisitor::default();
|
||||
visitor.visit_expr(&node.target);
|
||||
self.names.extend(visitor.names);
|
||||
}
|
||||
_ => {}
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
use ruff_python_ast::{self as ast, Stmt};
|
||||
use ruff_python_ast::Stmt;
|
||||
|
||||
use ruff_macros::{ViolationMetadata, derive_message_formats};
|
||||
use ruff_text_size::Ranged;
|
||||
@@ -71,15 +71,23 @@ fn walk_stmt(checker: &Checker, body: &[Stmt], f: fn(&Stmt) -> bool) {
|
||||
);
|
||||
}
|
||||
match stmt {
|
||||
Stmt::While(ast::StmtWhile { body, .. }) | Stmt::For(ast::StmtFor { body, .. }) => {
|
||||
walk_stmt(checker, body, Stmt::is_return_stmt);
|
||||
Stmt::While(node) => {
|
||||
walk_stmt(checker, &node.body, Stmt::is_return_stmt);
|
||||
}
|
||||
Stmt::If(ast::StmtIf { body, .. })
|
||||
| Stmt::Try(ast::StmtTry { body, .. })
|
||||
| Stmt::With(ast::StmtWith { body, .. }) => {
|
||||
walk_stmt(checker, body, f);
|
||||
Stmt::For(node) => {
|
||||
walk_stmt(checker, &node.body, Stmt::is_return_stmt);
|
||||
}
|
||||
Stmt::Match(ast::StmtMatch { cases, .. }) => {
|
||||
Stmt::If(node) => {
|
||||
walk_stmt(checker, &node.body, f);
|
||||
}
|
||||
Stmt::Try(node) => {
|
||||
walk_stmt(checker, &node.body, f);
|
||||
}
|
||||
Stmt::With(node) => {
|
||||
walk_stmt(checker, &node.body, f);
|
||||
}
|
||||
Stmt::Match(node) => {
|
||||
let cases = &node.cases;
|
||||
for case in cases {
|
||||
walk_stmt(checker, &case.body, f);
|
||||
}
|
||||
|
||||
@@ -5,8 +5,7 @@ use ruff_macros::{ViolationMetadata, derive_message_formats};
|
||||
use ruff_python_ast::comparable::ComparableExpr;
|
||||
use ruff_python_ast::name::UnqualifiedName;
|
||||
use ruff_python_ast::{
|
||||
Expr, ExprAttribute, ExprCall, ExprSubscript, ExprTuple, Stmt, StmtAssign, StmtAugAssign,
|
||||
StmtDelete, StmtFor, StmtIf,
|
||||
self as ast, Expr, ExprAttribute, ExprCall, ExprSubscript, ExprTuple, Stmt, StmtFor,
|
||||
visitor::{self, Visitor},
|
||||
};
|
||||
use ruff_text_size::TextRange;
|
||||
@@ -242,43 +241,39 @@ impl<'a> Visitor<'a> for LoopMutationsVisitor<'a> {
|
||||
fn visit_stmt(&mut self, stmt: &'a Stmt) {
|
||||
match stmt {
|
||||
// Ex) `del items[0]`
|
||||
Stmt::Delete(StmtDelete {
|
||||
range,
|
||||
targets,
|
||||
node_index: _,
|
||||
}) => {
|
||||
Stmt::Delete(node) => {
|
||||
let ast::StmtDelete {
|
||||
range,
|
||||
targets,
|
||||
node_index: _,
|
||||
} = &**node;
|
||||
self.handle_delete(*range, targets);
|
||||
visitor::walk_stmt(self, stmt);
|
||||
}
|
||||
|
||||
// Ex) `items[0] = 1`
|
||||
Stmt::Assign(StmtAssign { range, targets, .. }) => {
|
||||
self.handle_assign(*range, targets);
|
||||
Stmt::Assign(node) => {
|
||||
self.handle_assign(node.range, &node.targets);
|
||||
visitor::walk_stmt(self, stmt);
|
||||
}
|
||||
|
||||
// Ex) `items += [1]`
|
||||
Stmt::AugAssign(StmtAugAssign { range, target, .. }) => {
|
||||
self.handle_aug_assign(*range, target);
|
||||
Stmt::AugAssign(node) => {
|
||||
self.handle_aug_assign(node.range, &node.target);
|
||||
visitor::walk_stmt(self, stmt);
|
||||
}
|
||||
|
||||
// Ex) `if True: items.append(1)`
|
||||
Stmt::If(StmtIf {
|
||||
test,
|
||||
body,
|
||||
elif_else_clauses,
|
||||
..
|
||||
}) => {
|
||||
Stmt::If(node) => {
|
||||
// Handle the `if` branch.
|
||||
self.branch += 1;
|
||||
self.branches.push(self.branch);
|
||||
self.visit_expr(test);
|
||||
self.visit_body(body);
|
||||
self.visit_expr(&node.test);
|
||||
self.visit_body(&node.body);
|
||||
self.branches.pop();
|
||||
|
||||
// Handle the `elif` and `else` branches.
|
||||
for clause in elif_else_clauses {
|
||||
for clause in &node.elif_else_clauses {
|
||||
self.branch += 1;
|
||||
self.branches.push(self.branch);
|
||||
if let Some(test) = &clause.test {
|
||||
|
||||
@@ -119,13 +119,11 @@ impl<'a> Visitor<'a> for GroupNameFinder<'a> {
|
||||
return;
|
||||
}
|
||||
match stmt {
|
||||
Stmt::For(ast::StmtFor {
|
||||
target, iter, body, ..
|
||||
}) => {
|
||||
if self.name_matches(target) {
|
||||
Stmt::For(node) => {
|
||||
if self.name_matches(&node.target) {
|
||||
self.overridden = true;
|
||||
} else {
|
||||
if self.name_matches(iter) {
|
||||
if self.name_matches(&node.iter) {
|
||||
self.increment_usage_count(1);
|
||||
// This could happen when the group is being looped
|
||||
// over multiple times:
|
||||
@@ -136,36 +134,30 @@ impl<'a> Visitor<'a> for GroupNameFinder<'a> {
|
||||
// for item in group:
|
||||
// ...
|
||||
if self.usage_count > 1 {
|
||||
self.exprs.push(iter);
|
||||
self.exprs.push(&node.iter);
|
||||
}
|
||||
}
|
||||
self.nested = true;
|
||||
visitor::walk_body(self, body);
|
||||
visitor::walk_body(self, &node.body);
|
||||
self.nested = false;
|
||||
}
|
||||
}
|
||||
Stmt::While(ast::StmtWhile { body, .. }) => {
|
||||
Stmt::While(node) => {
|
||||
self.nested = true;
|
||||
visitor::walk_body(self, body);
|
||||
visitor::walk_body(self, &node.body);
|
||||
self.nested = false;
|
||||
}
|
||||
Stmt::If(ast::StmtIf {
|
||||
test,
|
||||
body,
|
||||
elif_else_clauses,
|
||||
range: _,
|
||||
node_index: _,
|
||||
}) => {
|
||||
Stmt::If(node) => {
|
||||
// base if plus branches
|
||||
let mut if_stack = Vec::with_capacity(1 + elif_else_clauses.len());
|
||||
let mut if_stack = Vec::with_capacity(1 + node.elif_else_clauses.len());
|
||||
// Initialize the vector with the count for the if branch.
|
||||
if_stack.push(0);
|
||||
self.counter_stack.push(if_stack);
|
||||
|
||||
self.visit_expr(test);
|
||||
self.visit_body(body);
|
||||
self.visit_expr(&node.test);
|
||||
self.visit_body(&node.body);
|
||||
|
||||
for clause in elif_else_clauses {
|
||||
for clause in &node.elif_else_clauses {
|
||||
self.counter_stack.last_mut().unwrap().push(0);
|
||||
self.visit_elif_else_clause(clause);
|
||||
}
|
||||
@@ -177,15 +169,10 @@ impl<'a> Visitor<'a> for GroupNameFinder<'a> {
|
||||
self.increment_usage_count(max_count);
|
||||
}
|
||||
}
|
||||
Stmt::Match(ast::StmtMatch {
|
||||
subject,
|
||||
cases,
|
||||
range: _,
|
||||
node_index: _,
|
||||
}) => {
|
||||
self.counter_stack.push(Vec::with_capacity(cases.len()));
|
||||
self.visit_expr(subject);
|
||||
for match_case in cases {
|
||||
Stmt::Match(node) => {
|
||||
self.counter_stack.push(Vec::with_capacity(node.cases.len()));
|
||||
self.visit_expr(&node.subject);
|
||||
for match_case in &node.cases {
|
||||
self.counter_stack.last_mut().unwrap().push(0);
|
||||
self.visit_match_case(match_case);
|
||||
}
|
||||
@@ -196,17 +183,17 @@ impl<'a> Visitor<'a> for GroupNameFinder<'a> {
|
||||
self.increment_usage_count(max_count);
|
||||
}
|
||||
}
|
||||
Stmt::Assign(ast::StmtAssign { targets, value, .. }) => {
|
||||
if targets.iter().any(|target| self.name_matches(target)) {
|
||||
Stmt::Assign(node) => {
|
||||
if node.targets.iter().any(|target| self.name_matches(target)) {
|
||||
self.overridden = true;
|
||||
} else {
|
||||
self.visit_expr(value);
|
||||
self.visit_expr(&node.value);
|
||||
}
|
||||
}
|
||||
Stmt::AnnAssign(ast::StmtAnnAssign { target, value, .. }) => {
|
||||
if self.name_matches(target) {
|
||||
Stmt::AnnAssign(node) => {
|
||||
if self.name_matches(&node.target) {
|
||||
self.overridden = true;
|
||||
} else if let Some(expr) = value {
|
||||
} else if let Some(expr) = &node.value {
|
||||
self.visit_expr(expr);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -66,7 +66,7 @@ impl AlwaysFixableViolation for SetAttrWithConstant {
|
||||
}
|
||||
|
||||
fn assignment(obj: &Expr, name: &str, value: &Expr, generator: Generator) -> String {
|
||||
let stmt = Stmt::Assign(ast::StmtAssign {
|
||||
let stmt = Stmt::Assign(Box::new(ast::StmtAssign {
|
||||
targets: vec![Expr::Attribute(ast::ExprAttribute {
|
||||
value: Box::new(obj.clone()),
|
||||
attr: Identifier::new(name.to_string(), TextRange::default()),
|
||||
@@ -77,7 +77,7 @@ fn assignment(obj: &Expr, name: &str, value: &Expr, generator: Generator) -> Str
|
||||
value: Box::new(value.clone()),
|
||||
range: TextRange::default(),
|
||||
node_index: ruff_python_ast::AtomicNodeIndex::NONE,
|
||||
});
|
||||
}));
|
||||
generator.stmt(&stmt)
|
||||
}
|
||||
|
||||
|
||||
@@ -59,16 +59,20 @@ pub(crate) fn all_with_model_form(checker: &Checker, class_def: &ast::StmtClassD
|
||||
}
|
||||
|
||||
for element in &class_def.body {
|
||||
let Stmt::ClassDef(ast::StmtClassDef { name, body, .. }) = element else {
|
||||
let Stmt::ClassDef(class_def_inner) = element else {
|
||||
continue;
|
||||
};
|
||||
let name = &class_def_inner.name;
|
||||
let body = &class_def_inner.body;
|
||||
if name != "Meta" {
|
||||
continue;
|
||||
}
|
||||
for element in body {
|
||||
let Stmt::Assign(ast::StmtAssign { targets, value, .. }) = element else {
|
||||
let Stmt::Assign(assign) = element else {
|
||||
continue;
|
||||
};
|
||||
let targets = &assign.targets;
|
||||
let value = &assign.value;
|
||||
for target in targets {
|
||||
let Expr::Name(ast::ExprName { id, .. }) = target else {
|
||||
continue;
|
||||
|
||||
@@ -57,16 +57,19 @@ pub(crate) fn exclude_with_model_form(checker: &Checker, class_def: &ast::StmtCl
|
||||
}
|
||||
|
||||
for element in &class_def.body {
|
||||
let Stmt::ClassDef(ast::StmtClassDef { name, body, .. }) = element else {
|
||||
let Stmt::ClassDef(class_def_inner) = element else {
|
||||
continue;
|
||||
};
|
||||
let name = &class_def_inner.name;
|
||||
let body = &class_def_inner.body;
|
||||
if name != "Meta" {
|
||||
continue;
|
||||
}
|
||||
for element in body {
|
||||
let Stmt::Assign(ast::StmtAssign { targets, .. }) = element else {
|
||||
let Stmt::Assign(assign) = element else {
|
||||
continue;
|
||||
};
|
||||
let targets = &assign.targets;
|
||||
for target in targets {
|
||||
let Expr::Name(ast::ExprName { id, .. }) = target else {
|
||||
continue;
|
||||
|
||||
@@ -72,7 +72,7 @@ pub(crate) fn model_without_dunder_str(checker: &Checker, class_def: &ast::StmtC
|
||||
fn has_dunder_method(class_def: &ast::StmtClassDef, semantic: &SemanticModel) -> bool {
|
||||
analyze::class::any_super_class(class_def, semantic, &|class_def| {
|
||||
class_def.body.iter().any(|val| match val {
|
||||
Stmt::FunctionDef(ast::StmtFunctionDef { name, .. }) => name == "__str__",
|
||||
Stmt::FunctionDef(node) => node.name.as_str() == "__str__",
|
||||
_ => false,
|
||||
})
|
||||
})
|
||||
@@ -90,24 +90,25 @@ fn is_non_abstract_model(class_def: &ast::StmtClassDef, semantic: &SemanticModel
|
||||
/// Check if class is abstract, in terms of Django model inheritance.
|
||||
fn is_model_abstract(class_def: &ast::StmtClassDef) -> bool {
|
||||
for element in &class_def.body {
|
||||
let Stmt::ClassDef(ast::StmtClassDef { name, body, .. }) = element else {
|
||||
let Stmt::ClassDef(node) = element else {
|
||||
continue;
|
||||
};
|
||||
if name != "Meta" {
|
||||
if node.name.as_str() != "Meta" {
|
||||
continue;
|
||||
}
|
||||
for element in body {
|
||||
for element in &node.body {
|
||||
match element {
|
||||
Stmt::Assign(ast::StmtAssign { targets, value, .. }) => {
|
||||
if targets
|
||||
Stmt::Assign(assign) => {
|
||||
if assign
|
||||
.targets
|
||||
.iter()
|
||||
.any(|target| is_abstract_true_assignment(target, Some(value)))
|
||||
.any(|target| is_abstract_true_assignment(target, Some(&assign.value)))
|
||||
{
|
||||
return true;
|
||||
}
|
||||
}
|
||||
Stmt::AnnAssign(ast::StmtAnnAssign { target, value, .. }) => {
|
||||
if is_abstract_true_assignment(target, value.as_deref()) {
|
||||
Stmt::AnnAssign(ann_assign) => {
|
||||
if is_abstract_true_assignment(&ann_assign.target, ann_assign.value.as_deref()) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
use ruff_python_ast::{self as ast, Expr, Stmt};
|
||||
use ruff_python_ast::{Expr, Stmt};
|
||||
|
||||
use ruff_macros::{ViolationMetadata, derive_message_formats};
|
||||
use ruff_python_ast::helpers::is_const_true;
|
||||
@@ -62,10 +62,13 @@ pub(crate) fn nullable_model_string_field(checker: &Checker, body: &[Stmt]) {
|
||||
|
||||
for statement in body {
|
||||
let value = match statement {
|
||||
Stmt::Assign(ast::StmtAssign { value, .. }) => value,
|
||||
Stmt::AnnAssign(ast::StmtAnnAssign {
|
||||
value: Some(value), ..
|
||||
}) => value,
|
||||
Stmt::Assign(assign) => &assign.value,
|
||||
Stmt::AnnAssign(ann_assign) => {
|
||||
match &ann_assign.value {
|
||||
Some(value) => value,
|
||||
None => continue,
|
||||
}
|
||||
}
|
||||
_ => continue,
|
||||
};
|
||||
|
||||
|
||||
@@ -153,13 +153,13 @@ impl fmt::Display for ContentType {
|
||||
|
||||
fn get_element_type(element: &Stmt, semantic: &SemanticModel) -> Option<ContentType> {
|
||||
match element {
|
||||
Stmt::Assign(ast::StmtAssign { targets, value, .. }) => {
|
||||
if let Expr::Call(ast::ExprCall { func, .. }) = value.as_ref() {
|
||||
Stmt::Assign(node) => {
|
||||
if let Expr::Call(ast::ExprCall { func, .. }) = node.value.as_ref() {
|
||||
if helpers::is_model_field(func, semantic) {
|
||||
return Some(ContentType::FieldDeclaration);
|
||||
}
|
||||
}
|
||||
let expr = targets.first()?;
|
||||
let expr = node.targets.first()?;
|
||||
let Expr::Name(ast::ExprName { id, .. }) = expr else {
|
||||
return None;
|
||||
};
|
||||
@@ -169,14 +169,14 @@ fn get_element_type(element: &Stmt, semantic: &SemanticModel) -> Option<ContentT
|
||||
None
|
||||
}
|
||||
}
|
||||
Stmt::ClassDef(ast::StmtClassDef { name, .. }) => {
|
||||
if name == "Meta" {
|
||||
Stmt::ClassDef(node) => {
|
||||
if node.name.as_str() == "Meta" {
|
||||
Some(ContentType::MetaClass)
|
||||
} else {
|
||||
None
|
||||
}
|
||||
}
|
||||
Stmt::FunctionDef(ast::StmtFunctionDef { name, .. }) => match name.as_str() {
|
||||
Stmt::FunctionDef(node) => match node.name.as_str() {
|
||||
name if is_dunder(name) => Some(ContentType::MagicMethod),
|
||||
"save" => Some(ContentType::SaveMethod),
|
||||
"get_absolute_url" => Some(ContentType::GetAbsoluteUrlMethod),
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
use ruff_python_ast::{Stmt, StmtTry};
|
||||
use ruff_python_ast::Stmt;
|
||||
use ruff_python_semantic::SemanticModel;
|
||||
use ruff_text_size::{Ranged, TextSize};
|
||||
|
||||
@@ -8,9 +8,10 @@ pub(super) fn outside_handlers(offset: TextSize, semantic: &SemanticModel) -> bo
|
||||
break;
|
||||
}
|
||||
|
||||
let Stmt::Try(StmtTry { handlers, .. }) = stmt else {
|
||||
let Stmt::Try(try_stmt) = stmt else {
|
||||
continue;
|
||||
};
|
||||
let handlers = &try_stmt.handlers;
|
||||
|
||||
if handlers
|
||||
.iter()
|
||||
|
||||
@@ -2,7 +2,7 @@ use rustc_hash::FxHashSet;
|
||||
|
||||
use ruff_macros::{ViolationMetadata, derive_message_formats};
|
||||
use ruff_python_ast::helpers::any_over_expr;
|
||||
use ruff_python_ast::{self as ast, Expr, Stmt};
|
||||
use ruff_python_ast::{Expr, Stmt};
|
||||
use ruff_text_size::Ranged;
|
||||
|
||||
use crate::checkers::ast::Checker;
|
||||
@@ -59,15 +59,15 @@ pub(crate) fn duplicate_class_field_definition(checker: &Checker, body: &[Stmt])
|
||||
for stmt in body {
|
||||
// Extract the property name from the assignment statement.
|
||||
let target = match stmt {
|
||||
Stmt::Assign(ast::StmtAssign { targets, .. }) => {
|
||||
if let [Expr::Name(id)] = targets.as_slice() {
|
||||
Stmt::Assign(assign_stmt) => {
|
||||
if let [Expr::Name(id)] = assign_stmt.targets.as_slice() {
|
||||
id
|
||||
} else {
|
||||
continue;
|
||||
}
|
||||
}
|
||||
Stmt::AnnAssign(ast::StmtAnnAssign { target, .. }) => {
|
||||
if let Expr::Name(id) = target.as_ref() {
|
||||
Stmt::AnnAssign(ann_assign_stmt) => {
|
||||
if let Expr::Name(id) = ann_assign_stmt.target.as_ref() {
|
||||
id
|
||||
} else {
|
||||
continue;
|
||||
@@ -78,20 +78,20 @@ pub(crate) fn duplicate_class_field_definition(checker: &Checker, body: &[Stmt])
|
||||
|
||||
// If this is an unrolled augmented assignment (e.g., `x = x + 1`), skip it.
|
||||
match stmt {
|
||||
Stmt::Assign(ast::StmtAssign { value, .. }) => {
|
||||
if any_over_expr(value.as_ref(), &|expr| {
|
||||
Stmt::Assign(assign_stmt) => {
|
||||
if any_over_expr(assign_stmt.value.as_ref(), &|expr| {
|
||||
expr.as_name_expr().is_some_and(|name| name.id == target.id)
|
||||
}) {
|
||||
continue;
|
||||
}
|
||||
}
|
||||
Stmt::AnnAssign(ast::StmtAnnAssign {
|
||||
value: Some(value), ..
|
||||
}) => {
|
||||
if any_over_expr(value.as_ref(), &|expr| {
|
||||
expr.as_name_expr().is_some_and(|name| name.id == target.id)
|
||||
}) {
|
||||
continue;
|
||||
Stmt::AnnAssign(ann_assign_stmt) => {
|
||||
if let Some(value) = &ann_assign_stmt.value {
|
||||
if any_over_expr(value.as_ref(), &|expr| {
|
||||
expr.as_name_expr().is_some_and(|name| name.id == target.id)
|
||||
}) {
|
||||
continue;
|
||||
}
|
||||
}
|
||||
}
|
||||
_ => continue,
|
||||
|
||||
@@ -58,11 +58,11 @@ impl Violation for NonUniqueEnums {
|
||||
pub(crate) fn non_unique_enums(checker: &Checker, parent: &Stmt, body: &[Stmt]) {
|
||||
let semantic = checker.semantic();
|
||||
|
||||
let Stmt::ClassDef(parent) = parent else {
|
||||
let Stmt::ClassDef(class_def) = parent else {
|
||||
return;
|
||||
};
|
||||
|
||||
if !parent.bases().iter().any(|expr| {
|
||||
if !class_def.bases().iter().any(|expr| {
|
||||
semantic
|
||||
.resolve_qualified_name(expr)
|
||||
.is_some_and(|qualified_name| matches!(qualified_name.segments(), ["enum", "Enum"]))
|
||||
@@ -72,9 +72,10 @@ pub(crate) fn non_unique_enums(checker: &Checker, parent: &Stmt, body: &[Stmt])
|
||||
|
||||
let mut seen_targets: FxHashSet<ComparableExpr> = FxHashSet::default();
|
||||
for stmt in body {
|
||||
let Stmt::Assign(ast::StmtAssign { value, .. }) = stmt else {
|
||||
let Stmt::Assign(assign_stmt) = stmt else {
|
||||
continue;
|
||||
};
|
||||
let value = &assign_stmt.value;
|
||||
|
||||
if is_call_to_enum_auto(semantic, value) {
|
||||
continue;
|
||||
|
||||
@@ -1,4 +1,3 @@
|
||||
use ruff_python_ast as ast;
|
||||
use ruff_python_ast::Stmt;
|
||||
|
||||
use ruff_macros::{ViolationMetadata, derive_message_formats};
|
||||
@@ -44,17 +43,15 @@ impl AlwaysFixableViolation for StrOrReprDefinedInStub {
|
||||
|
||||
/// PYI029
|
||||
pub(crate) fn str_or_repr_defined_in_stub(checker: &Checker, stmt: &Stmt) {
|
||||
let Stmt::FunctionDef(ast::StmtFunctionDef {
|
||||
name,
|
||||
decorator_list,
|
||||
returns,
|
||||
parameters,
|
||||
..
|
||||
}) = stmt
|
||||
else {
|
||||
let Stmt::FunctionDef(func_def) = stmt else {
|
||||
return;
|
||||
};
|
||||
|
||||
let name = &func_def.name;
|
||||
let decorator_list = &func_def.decorator_list;
|
||||
let returns = &func_def.returns;
|
||||
let parameters = &func_def.parameters;
|
||||
|
||||
let Some(returns) = returns else {
|
||||
return;
|
||||
};
|
||||
|
||||
@@ -196,15 +196,14 @@ pub(crate) fn unused_private_type_var(checker: &Checker, scope: &Scope) {
|
||||
let Some(source) = binding.source else {
|
||||
continue;
|
||||
};
|
||||
let stmt @ Stmt::Assign(ast::StmtAssign { targets, value, .. }) =
|
||||
checker.semantic().statement(source)
|
||||
else {
|
||||
let stmt = checker.semantic().statement(source);
|
||||
let Stmt::Assign(assign) = stmt else {
|
||||
continue;
|
||||
};
|
||||
let [Expr::Name(ast::ExprName { id, .. })] = &targets[..] else {
|
||||
let [Expr::Name(ast::ExprName { id, .. })] = &assign.targets[..] else {
|
||||
continue;
|
||||
};
|
||||
let Expr::Call(ast::ExprCall { func, .. }) = value.as_ref() else {
|
||||
let Expr::Call(ast::ExprCall { func, .. }) = assign.value.as_ref() else {
|
||||
continue;
|
||||
};
|
||||
|
||||
@@ -317,18 +316,16 @@ pub(crate) fn unused_private_type_alias(checker: &Checker, scope: &Scope) {
|
||||
|
||||
fn extract_type_alias_name<'a>(stmt: &'a ast::Stmt, semantic: &SemanticModel) -> Option<&'a str> {
|
||||
match stmt {
|
||||
ast::Stmt::AnnAssign(ast::StmtAnnAssign {
|
||||
target, annotation, ..
|
||||
}) => {
|
||||
let ast::ExprName { id, .. } = target.as_name_expr()?;
|
||||
if semantic.match_typing_expr(annotation, "TypeAlias") {
|
||||
ast::Stmt::AnnAssign(ann_assign) => {
|
||||
let ast::ExprName { id, .. } = ann_assign.target.as_name_expr()?;
|
||||
if semantic.match_typing_expr(&ann_assign.annotation, "TypeAlias") {
|
||||
Some(id)
|
||||
} else {
|
||||
None
|
||||
}
|
||||
}
|
||||
ast::Stmt::TypeAlias(ast::StmtTypeAlias { name, .. }) => {
|
||||
let ast::ExprName { id, .. } = name.as_name_expr()?;
|
||||
ast::Stmt::TypeAlias(type_alias) => {
|
||||
let ast::ExprName { id, .. } = type_alias.name.as_name_expr()?;
|
||||
Some(id)
|
||||
}
|
||||
_ => None,
|
||||
@@ -388,9 +385,9 @@ fn extract_typeddict_name<'a>(stmt: &'a Stmt, semantic: &SemanticModel) -> Optio
|
||||
// class Bar(typing.TypedDict, typing.Generic[T]):
|
||||
// y: T
|
||||
// ```
|
||||
Stmt::ClassDef(class_def @ ast::StmtClassDef { name, .. }) => {
|
||||
Stmt::ClassDef(class_def) => {
|
||||
if class_def.bases().iter().any(is_typeddict) {
|
||||
Some(name)
|
||||
Some(&class_def.name)
|
||||
} else {
|
||||
None
|
||||
}
|
||||
@@ -402,12 +399,12 @@ fn extract_typeddict_name<'a>(stmt: &'a Stmt, semantic: &SemanticModel) -> Optio
|
||||
// import typing
|
||||
// Baz = typing.TypedDict("Baz", {"z": bytes})
|
||||
// ```
|
||||
Stmt::Assign(ast::StmtAssign { targets, value, .. }) => {
|
||||
let [target] = targets.as_slice() else {
|
||||
Stmt::Assign(assign) => {
|
||||
let [target] = assign.targets.as_slice() else {
|
||||
return None;
|
||||
};
|
||||
let ast::ExprName { id, .. } = target.as_name_expr()?;
|
||||
let ast::ExprCall { func, .. } = value.as_call_expr()?;
|
||||
let ast::ExprCall { func, .. } = assign.value.as_call_expr()?;
|
||||
if is_typeddict(func) { Some(id) } else { None }
|
||||
}
|
||||
_ => None,
|
||||
|
||||
@@ -369,10 +369,10 @@ impl Violation for PytestUnittestRaisesAssertion {
|
||||
/// PT027
|
||||
pub(crate) fn unittest_raises_assertion_call(checker: &Checker, call: &ast::ExprCall) {
|
||||
// Bindings in `with` statements are handled by `unittest_raises_assertion_bindings`.
|
||||
if let Stmt::With(ast::StmtWith { items, .. }) = checker.semantic().current_statement() {
|
||||
if let Stmt::With(with_stmt) = checker.semantic().current_statement() {
|
||||
let call_ref = AnyNodeRef::from(call);
|
||||
|
||||
if items.iter().any(|item| {
|
||||
if with_stmt.items.iter().any(|item| {
|
||||
AnyNodeRef::from(&item.context_expr).ptr_eq(call_ref) && item.optional_vars.is_some()
|
||||
}) {
|
||||
return;
|
||||
@@ -390,7 +390,11 @@ pub(crate) fn unittest_raises_assertion_binding(checker: &Checker, binding: &Bin
|
||||
|
||||
let semantic = checker.semantic();
|
||||
|
||||
let Some(Stmt::With(with)) = binding.statement(semantic) else {
|
||||
let Some(stmt) = binding.statement(semantic) else {
|
||||
return;
|
||||
};
|
||||
|
||||
let Stmt::With(with) = stmt else {
|
||||
return;
|
||||
};
|
||||
|
||||
|
||||
@@ -220,11 +220,11 @@ pub(crate) fn complex_raises(checker: &Checker, stmt: &Stmt, items: &[WithItem],
|
||||
if raises_called {
|
||||
let is_too_complex = if let [stmt] = body {
|
||||
match stmt {
|
||||
Stmt::With(ast::StmtWith { body, .. }) => is_non_trivial_with_body(body),
|
||||
Stmt::With(with_stmt) => is_non_trivial_with_body(&with_stmt.body),
|
||||
// Allow function and class definitions to test decorators.
|
||||
Stmt::ClassDef(_) | Stmt::FunctionDef(_) => false,
|
||||
// Allow empty `for` loops to test iterators.
|
||||
Stmt::For(ast::StmtFor { body, .. }) => match &body[..] {
|
||||
Stmt::For(for_stmt) => match &for_stmt.body[..] {
|
||||
[Stmt::Pass(_)] => false,
|
||||
[Stmt::Expr(ast::StmtExpr { value, .. })] => !value.is_ellipsis_literal_expr(),
|
||||
_ => true,
|
||||
|
||||
@@ -162,12 +162,12 @@ impl TryFrom<&str> for UnittestAssert {
|
||||
}
|
||||
|
||||
fn assert(expr: &Expr, msg: Option<&Expr>) -> Stmt {
|
||||
Stmt::Assert(ast::StmtAssert {
|
||||
Stmt::Assert(Box::new(ast::StmtAssert {
|
||||
test: Box::new(expr.clone()),
|
||||
msg: msg.map(|msg| Box::new(msg.clone())),
|
||||
range: TextRange::default(),
|
||||
node_index: ruff_python_ast::AtomicNodeIndex::NONE,
|
||||
})
|
||||
}))
|
||||
}
|
||||
|
||||
fn compare(left: &Expr, cmp_op: CmpOp, right: &Expr) -> Expr {
|
||||
|
||||
@@ -206,11 +206,11 @@ pub(crate) fn complex_warns(checker: &Checker, stmt: &Stmt, items: &[WithItem],
|
||||
if warns_called {
|
||||
let is_too_complex = if let [stmt] = body {
|
||||
match stmt {
|
||||
Stmt::With(ast::StmtWith { body, .. }) => is_non_trivial_with_body(body),
|
||||
Stmt::With(with_stmt) => is_non_trivial_with_body(&with_stmt.body),
|
||||
// Allow function and class definitions to test decorators.
|
||||
Stmt::ClassDef(_) | Stmt::FunctionDef(_) => false,
|
||||
// Allow empty `for` loops to test iterators.
|
||||
Stmt::For(ast::StmtFor { body, .. }) => match &body[..] {
|
||||
Stmt::For(for_stmt) => match &for_stmt.body[..] {
|
||||
[Stmt::Pass(_)] => false,
|
||||
[Stmt::Expr(ast::StmtExpr { value, .. })] => !value.is_ellipsis_literal_expr(),
|
||||
_ => true,
|
||||
|
||||
@@ -448,12 +448,12 @@ fn is_noreturn_func(func: &Expr, semantic: &SemanticModel) -> bool {
|
||||
return false;
|
||||
};
|
||||
|
||||
let Stmt::FunctionDef(ast::StmtFunctionDef { returns, .. }) = semantic.statement(node_id)
|
||||
let Stmt::FunctionDef(node) = semantic.statement(node_id)
|
||||
else {
|
||||
return false;
|
||||
};
|
||||
|
||||
let Some(returns) = returns.as_ref() else {
|
||||
let Some(returns) = node.returns.as_ref() else {
|
||||
return false;
|
||||
};
|
||||
|
||||
@@ -481,19 +481,16 @@ fn add_return_none(checker: &Checker, stmt: &Stmt, range: TextRange) {
|
||||
|
||||
fn has_implicit_return(checker: &Checker, stmt: &Stmt) -> bool {
|
||||
match stmt {
|
||||
Stmt::If(ast::StmtIf {
|
||||
body,
|
||||
elif_else_clauses,
|
||||
..
|
||||
}) => {
|
||||
if body
|
||||
Stmt::If(node) => {
|
||||
if node
|
||||
.body
|
||||
.last()
|
||||
.is_some_and(|last| has_implicit_return(checker, last))
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
if elif_else_clauses.iter().any(|clause| {
|
||||
if node.elif_else_clauses.iter().any(|clause| {
|
||||
clause
|
||||
.body
|
||||
.last()
|
||||
@@ -504,25 +501,33 @@ fn has_implicit_return(checker: &Checker, stmt: &Stmt) -> bool {
|
||||
|
||||
// Check if we don't have an else clause
|
||||
matches!(
|
||||
elif_else_clauses.last(),
|
||||
node.elif_else_clauses.last(),
|
||||
None | Some(ast::ElifElseClause { test: Some(_), .. })
|
||||
)
|
||||
}
|
||||
Stmt::Assert(ast::StmtAssert { test, .. }) if is_const_false(test) => false,
|
||||
Stmt::While(ast::StmtWhile { test, .. }) if is_const_true(test) => false,
|
||||
Stmt::For(ast::StmtFor { orelse, .. }) | Stmt::While(ast::StmtWhile { orelse, .. }) => {
|
||||
if let Some(last_stmt) = orelse.last() {
|
||||
Stmt::Assert(node) if is_const_false(&node.test) => false,
|
||||
Stmt::While(node) if is_const_true(&node.test) => false,
|
||||
Stmt::For(node) => {
|
||||
if let Some(last_stmt) = node.orelse.last() {
|
||||
has_implicit_return(checker, last_stmt)
|
||||
} else {
|
||||
true
|
||||
}
|
||||
}
|
||||
Stmt::Match(ast::StmtMatch { cases, .. }) => cases.iter().any(|case| {
|
||||
Stmt::While(node) => {
|
||||
if let Some(last_stmt) = node.orelse.last() {
|
||||
has_implicit_return(checker, last_stmt)
|
||||
} else {
|
||||
true
|
||||
}
|
||||
}
|
||||
Stmt::Match(node) => node.cases.iter().any(|case| {
|
||||
case.body
|
||||
.last()
|
||||
.is_some_and(|last| has_implicit_return(checker, last))
|
||||
}),
|
||||
Stmt::With(ast::StmtWith { body, .. }) => body
|
||||
Stmt::With(node) => node
|
||||
.body
|
||||
.last()
|
||||
.is_some_and(|last_stmt| has_implicit_return(checker, last_stmt)),
|
||||
Stmt::Return(_) | Stmt::Raise(_) | Stmt::Try(_) => false,
|
||||
|
||||
@@ -62,11 +62,11 @@ impl<'semantic, 'data> ReturnVisitor<'semantic, 'data> {
|
||||
impl<'a> Visitor<'a> for ReturnVisitor<'_, 'a> {
|
||||
fn visit_stmt(&mut self, stmt: &'a Stmt) {
|
||||
match stmt {
|
||||
Stmt::ClassDef(ast::StmtClassDef { decorator_list, .. }) => {
|
||||
Stmt::ClassDef(node) => {
|
||||
// Visit the decorators, etc.
|
||||
self.sibling = Some(stmt);
|
||||
self.parents.push(stmt);
|
||||
for decorator in decorator_list {
|
||||
for decorator in &node.decorator_list {
|
||||
visitor::walk_decorator(self, decorator);
|
||||
}
|
||||
self.parents.pop();
|
||||
@@ -74,12 +74,15 @@ impl<'a> Visitor<'a> for ReturnVisitor<'_, 'a> {
|
||||
// But don't recurse into the body.
|
||||
return;
|
||||
}
|
||||
Stmt::FunctionDef(ast::StmtFunctionDef {
|
||||
parameters,
|
||||
decorator_list,
|
||||
returns,
|
||||
..
|
||||
}) => {
|
||||
Stmt::FunctionDef(node) => {
|
||||
let ast::StmtFunctionDef {
|
||||
parameters,
|
||||
decorator_list,
|
||||
returns,
|
||||
range: _,
|
||||
node_index: _,
|
||||
..
|
||||
} = &**node;
|
||||
// Visit the decorators, etc.
|
||||
self.sibling = Some(stmt);
|
||||
self.parents.push(stmt);
|
||||
@@ -95,24 +98,30 @@ impl<'a> Visitor<'a> for ReturnVisitor<'_, 'a> {
|
||||
// But don't recurse into the body.
|
||||
return;
|
||||
}
|
||||
Stmt::Global(ast::StmtGlobal {
|
||||
names,
|
||||
range: _,
|
||||
node_index: _,
|
||||
})
|
||||
| Stmt::Nonlocal(ast::StmtNonlocal {
|
||||
names,
|
||||
range: _,
|
||||
node_index: _,
|
||||
}) => {
|
||||
Stmt::Global(node) => {
|
||||
let ast::StmtGlobal {
|
||||
names,
|
||||
range: _,
|
||||
node_index: _,
|
||||
} = &**node;
|
||||
self.stack
|
||||
.non_locals
|
||||
.extend(names.iter().map(Identifier::as_str));
|
||||
}
|
||||
Stmt::AnnAssign(ast::StmtAnnAssign { target, value, .. }) => {
|
||||
Stmt::Nonlocal(node) => {
|
||||
let ast::StmtNonlocal {
|
||||
names,
|
||||
range: _,
|
||||
node_index: _,
|
||||
} = &**node;
|
||||
self.stack
|
||||
.non_locals
|
||||
.extend(names.iter().map(Identifier::as_str));
|
||||
}
|
||||
Stmt::AnnAssign(node) => {
|
||||
// Ex) `x: int`
|
||||
if value.is_none() {
|
||||
if let Expr::Name(name) = target.as_ref() {
|
||||
if node.value.is_none() {
|
||||
if let Expr::Name(name) = node.target.as_ref() {
|
||||
self.stack.annotations.insert(name.id.as_str());
|
||||
}
|
||||
}
|
||||
@@ -140,11 +149,11 @@ impl<'a> Visitor<'a> for ReturnVisitor<'_, 'a> {
|
||||
// x = f.read()
|
||||
// return x
|
||||
// ```
|
||||
Stmt::With(with) => {
|
||||
Stmt::With(with_node) => {
|
||||
if let Some(stmt_assign) =
|
||||
with.body.last().and_then(Stmt::as_assign_stmt)
|
||||
with_node.body.last().and_then(Stmt::as_assign_stmt)
|
||||
{
|
||||
if !has_conditional_body(with, self.semantic) {
|
||||
if !has_conditional_body(with_node, self.semantic) {
|
||||
self.stack.assignment_return.push((
|
||||
stmt_assign,
|
||||
stmt_return,
|
||||
@@ -159,11 +168,14 @@ impl<'a> Visitor<'a> for ReturnVisitor<'_, 'a> {
|
||||
|
||||
self.stack.returns.push(stmt_return);
|
||||
}
|
||||
Stmt::If(ast::StmtIf {
|
||||
body,
|
||||
elif_else_clauses,
|
||||
..
|
||||
}) => {
|
||||
Stmt::If(node) => {
|
||||
let ast::StmtIf {
|
||||
body,
|
||||
elif_else_clauses,
|
||||
range: _,
|
||||
node_index: _,
|
||||
..
|
||||
} = &**node;
|
||||
if let Some(first) = elif_else_clauses.first() {
|
||||
self.stack.elifs_elses.push((body, first));
|
||||
}
|
||||
|
||||
@@ -140,10 +140,10 @@ fn is_dunder_method(name: &str) -> bool {
|
||||
}
|
||||
|
||||
fn is_exception_check(stmt: &Stmt) -> bool {
|
||||
let Stmt::If(ast::StmtIf { body, .. }) = stmt else {
|
||||
let Stmt::If(node) = stmt else {
|
||||
return false;
|
||||
};
|
||||
matches!(body.as_slice(), [Stmt::Raise(_)])
|
||||
matches!(node.body.as_slice(), [Stmt::Raise(_)])
|
||||
}
|
||||
|
||||
/// SIM201
|
||||
|
||||
@@ -68,18 +68,10 @@ impl Violation for MultipleWithStatements {
|
||||
/// Returns a boolean indicating whether it's an async with statement, the items
|
||||
/// and body.
|
||||
fn next_with(body: &[Stmt]) -> Option<(bool, &[WithItem], &[Stmt])> {
|
||||
let [
|
||||
Stmt::With(ast::StmtWith {
|
||||
is_async,
|
||||
items,
|
||||
body,
|
||||
..
|
||||
}),
|
||||
] = body
|
||||
else {
|
||||
let [Stmt::With(node)] = body else {
|
||||
return None;
|
||||
};
|
||||
Some((*is_async, items, body))
|
||||
Some((node.is_async, &node.items, &node.body))
|
||||
}
|
||||
|
||||
/// Check if `with_items` contains a single item which should not necessarily be
|
||||
@@ -139,8 +131,8 @@ pub(crate) fn multiple_with_statements(
|
||||
// with B(), C():
|
||||
// print("hello")
|
||||
// ```
|
||||
if let Some(Stmt::With(ast::StmtWith { body, .. })) = with_parent {
|
||||
if body.len() == 1 {
|
||||
if let Some(Stmt::With(node)) = with_parent {
|
||||
if node.body.len() == 1 {
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -230,21 +230,13 @@ fn nested_if_body(stmt_if: &ast::StmtIf) -> Option<NestedIf<'_>> {
|
||||
/// ...
|
||||
/// ```
|
||||
fn find_last_nested_if(body: &[Stmt]) -> Option<&Expr> {
|
||||
let [
|
||||
Stmt::If(ast::StmtIf {
|
||||
test,
|
||||
body: inner_body,
|
||||
elif_else_clauses,
|
||||
..
|
||||
}),
|
||||
] = body
|
||||
else {
|
||||
let [Stmt::If(node)] = body else {
|
||||
return None;
|
||||
};
|
||||
if !elif_else_clauses.is_empty() {
|
||||
if !node.elif_else_clauses.is_empty() {
|
||||
return None;
|
||||
}
|
||||
find_last_nested_if(inner_body).or(Some(test))
|
||||
find_last_nested_if(&node.body).or(Some(&node.test))
|
||||
}
|
||||
|
||||
/// Returns `true` if an expression is an `if __name__ == "__main__":` check.
|
||||
|
||||
@@ -165,20 +165,18 @@ pub(crate) fn enumerate_for_loop(checker: &Checker, for_stmt: &ast::StmtFor) {
|
||||
/// If the statement is an index increment statement (e.g., `i += 1`), return
|
||||
/// the name of the index variable.
|
||||
fn match_index_increment(stmt: &Stmt) -> Option<&ast::ExprName> {
|
||||
let Stmt::AugAssign(ast::StmtAugAssign {
|
||||
target,
|
||||
op: Operator::Add,
|
||||
value,
|
||||
..
|
||||
}) = stmt
|
||||
else {
|
||||
let Stmt::AugAssign(node) = stmt else {
|
||||
return None;
|
||||
};
|
||||
|
||||
let name = target.as_name_expr()?;
|
||||
if !matches!(node.op, Operator::Add) {
|
||||
return None;
|
||||
}
|
||||
|
||||
let name = node.target.as_name_expr()?;
|
||||
|
||||
if matches!(
|
||||
value.as_ref(),
|
||||
node.value.as_ref(),
|
||||
Expr::NumberLiteral(ast::ExprNumberLiteral {
|
||||
value: Number::Int(Int::ONE),
|
||||
..
|
||||
|
||||
@@ -98,26 +98,16 @@ pub(crate) fn if_else_block_instead_of_dict_get(checker: &Checker, stmt_if: &ast
|
||||
let [else_body_stmt] = else_body.as_slice() else {
|
||||
return;
|
||||
};
|
||||
let Stmt::Assign(ast::StmtAssign {
|
||||
targets: body_var,
|
||||
value: body_value,
|
||||
..
|
||||
}) = &body_stmt
|
||||
else {
|
||||
let Stmt::Assign(body_node) = &body_stmt else {
|
||||
return;
|
||||
};
|
||||
let [body_var] = body_var.as_slice() else {
|
||||
let [body_var] = body_node.targets.as_slice() else {
|
||||
return;
|
||||
};
|
||||
let Stmt::Assign(ast::StmtAssign {
|
||||
targets: orelse_var,
|
||||
value: orelse_value,
|
||||
..
|
||||
}) = &else_body_stmt
|
||||
else {
|
||||
let Stmt::Assign(orelse_node) = &else_body_stmt else {
|
||||
return;
|
||||
};
|
||||
let [orelse_var] = orelse_var.as_slice() else {
|
||||
let [orelse_var] = orelse_node.targets.as_slice() else {
|
||||
return;
|
||||
};
|
||||
|
||||
@@ -143,8 +133,8 @@ pub(crate) fn if_else_block_instead_of_dict_get(checker: &Checker, stmt_if: &ast
|
||||
}
|
||||
|
||||
let (expected_var, expected_value, default_var, default_value) = match ops[..] {
|
||||
[CmpOp::In] => (body_var, body_value, orelse_var, orelse_value.as_ref()),
|
||||
[CmpOp::NotIn] => (orelse_var, orelse_value, body_var, body_value.as_ref()),
|
||||
[CmpOp::In] => (body_var, &body_node.value, orelse_var, orelse_node.value.as_ref()),
|
||||
[CmpOp::NotIn] => (orelse_var, &orelse_node.value, body_var, body_node.value.as_ref()),
|
||||
_ => {
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -112,27 +112,13 @@ pub(crate) fn if_else_block_instead_of_if_exp(checker: &Checker, stmt_if: &ast::
|
||||
else {
|
||||
return;
|
||||
};
|
||||
let [
|
||||
Stmt::Assign(ast::StmtAssign {
|
||||
targets: body_targets,
|
||||
value: body_value,
|
||||
..
|
||||
}),
|
||||
] = body.as_slice()
|
||||
else {
|
||||
let [Stmt::Assign(body_node)] = body.as_slice() else {
|
||||
return;
|
||||
};
|
||||
let [
|
||||
Stmt::Assign(ast::StmtAssign {
|
||||
targets: else_targets,
|
||||
value: else_value,
|
||||
..
|
||||
}),
|
||||
] = else_body.as_slice()
|
||||
else {
|
||||
let [Stmt::Assign(else_node)] = else_body.as_slice() else {
|
||||
return;
|
||||
};
|
||||
let ([body_target], [else_target]) = (body_targets.as_slice(), else_targets.as_slice()) else {
|
||||
let ([body_target], [else_target]) = (body_node.targets.as_slice(), else_node.targets.as_slice()) else {
|
||||
return;
|
||||
};
|
||||
let Expr::Name(ast::ExprName { id: body_id, .. }) = body_target else {
|
||||
@@ -148,13 +134,13 @@ pub(crate) fn if_else_block_instead_of_if_exp(checker: &Checker, stmt_if: &ast::
|
||||
// Avoid suggesting ternary for `if (yield ...)`-style checks.
|
||||
// TODO(charlie): Fix precedence handling for yields in generator.
|
||||
if matches!(
|
||||
body_value.as_ref(),
|
||||
body_node.value.as_ref(),
|
||||
Expr::Yield(_) | Expr::YieldFrom(_) | Expr::Await(_)
|
||||
) {
|
||||
return;
|
||||
}
|
||||
if matches!(
|
||||
else_value.as_ref(),
|
||||
else_node.value.as_ref(),
|
||||
Expr::Yield(_) | Expr::YieldFrom(_) | Expr::Await(_)
|
||||
) {
|
||||
return;
|
||||
@@ -190,20 +176,20 @@ pub(crate) fn if_else_block_instead_of_if_exp(checker: &Checker, stmt_if: &ast::
|
||||
// - If `test == not body_value`, replace with `target_var = body_value and else_value`
|
||||
// - If `not test == body_value`, replace with `target_var = body_value and else_value`
|
||||
// - Otherwise, replace with `target_var = body_value if test else else_value`
|
||||
let (contents, assignment_kind) = match (test, body_value) {
|
||||
(test_node, body_node)
|
||||
if ComparableExpr::from(test_node) == ComparableExpr::from(body_node)
|
||||
let (contents, assignment_kind) = match (test, &body_node.value) {
|
||||
(test_node, body_val_node)
|
||||
if ComparableExpr::from(test_node) == ComparableExpr::from(body_val_node.as_ref())
|
||||
&& !contains_effect(test_node, |id| checker.semantic().has_builtin_binding(id)) =>
|
||||
{
|
||||
let target_var = &body_target;
|
||||
let binary = assignment_binary_or(target_var, body_value, else_value);
|
||||
let binary = assignment_binary_or(target_var, &body_node.value, &else_node.value);
|
||||
(checker.generator().stmt(&binary), AssignmentKind::Binary)
|
||||
}
|
||||
(test_node, body_node)
|
||||
(test_node, body_val_node)
|
||||
if (test_node.as_unary_op_expr().is_some_and(|op_expr| {
|
||||
op_expr.op.is_not()
|
||||
&& ComparableExpr::from(&op_expr.operand) == ComparableExpr::from(body_node)
|
||||
}) || body_node.as_unary_op_expr().is_some_and(|op_expr| {
|
||||
&& ComparableExpr::from(&op_expr.operand) == ComparableExpr::from(body_val_node.as_ref())
|
||||
}) || body_val_node.as_ref().as_unary_op_expr().is_some_and(|op_expr| {
|
||||
op_expr.op.is_not()
|
||||
&& ComparableExpr::from(&op_expr.operand) == ComparableExpr::from(test_node)
|
||||
})) && !contains_effect(test_node, |id| {
|
||||
@@ -211,12 +197,12 @@ pub(crate) fn if_else_block_instead_of_if_exp(checker: &Checker, stmt_if: &ast::
|
||||
}) =>
|
||||
{
|
||||
let target_var = &body_target;
|
||||
let binary = assignment_binary_and(target_var, body_value, else_value);
|
||||
let binary = assignment_binary_and(target_var, &body_node.value, &else_node.value);
|
||||
(checker.generator().stmt(&binary), AssignmentKind::Binary)
|
||||
}
|
||||
_ => {
|
||||
let target_var = &body_target;
|
||||
let ternary = assignment_ternary(target_var, body_value, test, else_value);
|
||||
let ternary = assignment_ternary(target_var, &body_node.value, test, &else_node.value);
|
||||
(checker.generator().stmt(&ternary), AssignmentKind::Ternary)
|
||||
}
|
||||
};
|
||||
|
||||
@@ -92,15 +92,9 @@ impl Violation for NeedlessBool {
|
||||
/// SIM103
|
||||
pub(crate) fn needless_bool(checker: &Checker, stmt: &Stmt) {
|
||||
let Stmt::If(stmt_if) = stmt else { return };
|
||||
let ast::StmtIf {
|
||||
test: if_test,
|
||||
body: if_body,
|
||||
elif_else_clauses,
|
||||
..
|
||||
} = stmt_if;
|
||||
|
||||
// Extract an `if` or `elif` (that returns) followed by an else (that returns the same value)
|
||||
let (if_test, if_body, else_body, range) = match elif_else_clauses.as_slice() {
|
||||
let (if_test, if_body, else_body, range) = match stmt_if.elif_else_clauses.as_slice() {
|
||||
// if-else case:
|
||||
// ```python
|
||||
// if x > 0:
|
||||
@@ -115,8 +109,8 @@ pub(crate) fn needless_bool(checker: &Checker, stmt: &Stmt) {
|
||||
..
|
||||
},
|
||||
] => (
|
||||
if_test.as_ref(),
|
||||
if_body,
|
||||
stmt_if.test.as_ref(),
|
||||
stmt_if.body.as_slice(),
|
||||
else_body.as_slice(),
|
||||
stmt_if.range(),
|
||||
),
|
||||
@@ -143,7 +137,7 @@ pub(crate) fn needless_bool(checker: &Checker, stmt: &Stmt) {
|
||||
},
|
||||
] => (
|
||||
elif_test,
|
||||
elif_body,
|
||||
elif_body.as_slice(),
|
||||
else_body.as_slice(),
|
||||
TextRange::new(elif_range.start(), else_range.end()),
|
||||
),
|
||||
@@ -155,7 +149,7 @@ pub(crate) fn needless_bool(checker: &Checker, stmt: &Stmt) {
|
||||
// ```
|
||||
[] => {
|
||||
// Fetching the next sibling is expensive, so do some validation early.
|
||||
if is_one_line_return_bool(if_body).is_none() {
|
||||
if is_one_line_return_bool(&stmt_if.body).is_none() {
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -175,8 +169,8 @@ pub(crate) fn needless_bool(checker: &Checker, stmt: &Stmt) {
|
||||
}
|
||||
|
||||
(
|
||||
if_test.as_ref(),
|
||||
if_body,
|
||||
stmt_if.test.as_ref(),
|
||||
stmt_if.body.as_slice(),
|
||||
std::slice::from_ref(next_stmt),
|
||||
TextRange::new(stmt_if.start(), next_stmt.end()),
|
||||
)
|
||||
@@ -231,7 +225,7 @@ pub(crate) fn needless_bool(checker: &Checker, stmt: &Stmt) {
|
||||
op: ast::UnaryOp::Not,
|
||||
operand,
|
||||
..
|
||||
}) => Some((**operand).clone()),
|
||||
}) => Some(operand.clone()),
|
||||
|
||||
Expr::Compare(ast::ExprCompare {
|
||||
ops,
|
||||
@@ -252,26 +246,26 @@ pub(crate) fn needless_bool(checker: &Checker, stmt: &Stmt) {
|
||||
unreachable!("Single comparison with multiple comparators");
|
||||
};
|
||||
|
||||
Some(Expr::Compare(ast::ExprCompare {
|
||||
Some(Box::new(Expr::Compare(ast::ExprCompare {
|
||||
ops: Box::new([op.negate()]),
|
||||
left: left.clone(),
|
||||
comparators: Box::new([right.clone()]),
|
||||
range: TextRange::default(),
|
||||
node_index: ruff_python_ast::AtomicNodeIndex::NONE,
|
||||
}))
|
||||
})))
|
||||
}
|
||||
|
||||
_ => Some(Expr::UnaryOp(ast::ExprUnaryOp {
|
||||
_ => Some(Box::new(Expr::UnaryOp(ast::ExprUnaryOp {
|
||||
op: ast::UnaryOp::Not,
|
||||
operand: Box::new(if_test.clone()),
|
||||
range: TextRange::default(),
|
||||
node_index: ruff_python_ast::AtomicNodeIndex::NONE,
|
||||
})),
|
||||
}))),
|
||||
}
|
||||
} else if if_test.is_compare_expr() {
|
||||
// If the condition is a comparison, we can replace it with the condition, since we
|
||||
// know it's a boolean.
|
||||
Some(if_test.clone())
|
||||
Some(Box::new(if_test.clone()))
|
||||
} else if checker.semantic().has_builtin_binding("bool") {
|
||||
// Otherwise, we need to wrap the condition in a call to `bool`.
|
||||
let func_node = ast::ExprName {
|
||||
@@ -291,7 +285,7 @@ pub(crate) fn needless_bool(checker: &Checker, stmt: &Stmt) {
|
||||
range: TextRange::default(),
|
||||
node_index: ruff_python_ast::AtomicNodeIndex::NONE,
|
||||
};
|
||||
Some(Expr::Call(call_node))
|
||||
Some(Box::new(Expr::Call(call_node)))
|
||||
} else {
|
||||
None
|
||||
}
|
||||
@@ -300,7 +294,7 @@ pub(crate) fn needless_bool(checker: &Checker, stmt: &Stmt) {
|
||||
// Generate the replacement `return` statement.
|
||||
let replacement = condition.as_ref().map(|expr| {
|
||||
Stmt::Return(ast::StmtReturn {
|
||||
value: Some(Box::new(expr.clone())),
|
||||
value: Some(expr.clone()),
|
||||
range: TextRange::default(),
|
||||
node_index: ruff_python_ast::AtomicNodeIndex::NONE,
|
||||
})
|
||||
|
||||
@@ -68,8 +68,8 @@ fn match_async_exit_stack(semantic: &SemanticModel) -> bool {
|
||||
return false;
|
||||
}
|
||||
for parent in semantic.current_statements() {
|
||||
if let Stmt::With(ast::StmtWith { items, .. }) = parent {
|
||||
for item in items {
|
||||
if let Stmt::With(node) = parent {
|
||||
for item in &node.items {
|
||||
if let Expr::Call(ast::ExprCall { func, .. }) = &item.context_expr {
|
||||
if semantic
|
||||
.resolve_qualified_name(func)
|
||||
@@ -102,8 +102,8 @@ fn match_exit_stack(semantic: &SemanticModel) -> bool {
|
||||
return false;
|
||||
}
|
||||
for parent in semantic.current_statements() {
|
||||
if let Stmt::With(ast::StmtWith { items, .. }) = parent {
|
||||
for item in items {
|
||||
if let Stmt::With(node) = parent {
|
||||
for item in &node.items {
|
||||
if let Expr::Call(ast::ExprCall { func, .. }) = &item.context_expr {
|
||||
if semantic
|
||||
.resolve_qualified_name(func)
|
||||
|
||||
@@ -269,27 +269,25 @@ struct Terminal<'a> {
|
||||
}
|
||||
|
||||
fn match_loop(stmt: &Stmt) -> Option<Loop<'_>> {
|
||||
let Stmt::For(ast::StmtFor {
|
||||
body, target, iter, ..
|
||||
}) = stmt
|
||||
else {
|
||||
let Stmt::For(for_stmt) = stmt else {
|
||||
return None;
|
||||
};
|
||||
let ast::StmtFor {
|
||||
body, target, iter, ..
|
||||
} = &**for_stmt;
|
||||
|
||||
// The loop itself should contain a single `if` statement, with a single `return` statement in
|
||||
// the body.
|
||||
let [
|
||||
Stmt::If(ast::StmtIf {
|
||||
body: nested_body,
|
||||
test: nested_test,
|
||||
elif_else_clauses: nested_elif_else_clauses,
|
||||
range: _,
|
||||
node_index: _,
|
||||
}),
|
||||
] = body.as_slice()
|
||||
else {
|
||||
let [Stmt::If(if_stmt)] = body.as_slice() else {
|
||||
return None;
|
||||
};
|
||||
let ast::StmtIf {
|
||||
body: nested_body,
|
||||
test: nested_test,
|
||||
elif_else_clauses: nested_elif_else_clauses,
|
||||
range: _,
|
||||
node_index: _,
|
||||
} = &**if_stmt;
|
||||
if !nested_elif_else_clauses.is_empty() {
|
||||
return None;
|
||||
}
|
||||
@@ -326,9 +324,10 @@ fn match_loop(stmt: &Stmt) -> Option<Loop<'_>> {
|
||||
/// return False
|
||||
/// ```
|
||||
fn match_else_return(stmt: &Stmt) -> Option<Terminal<'_>> {
|
||||
let Stmt::For(ast::StmtFor { orelse, .. }) = stmt else {
|
||||
let Stmt::For(for_stmt) = stmt else {
|
||||
return None;
|
||||
};
|
||||
let ast::StmtFor { orelse, .. } = &**for_stmt;
|
||||
|
||||
// The `else` block has to contain a single `return True` or `return False`.
|
||||
let [
|
||||
@@ -366,9 +365,10 @@ fn match_else_return(stmt: &Stmt) -> Option<Terminal<'_>> {
|
||||
/// return False
|
||||
/// ```
|
||||
fn match_sibling_return<'a>(stmt: &'a Stmt, sibling: &'a Stmt) -> Option<Terminal<'a>> {
|
||||
let Stmt::For(ast::StmtFor { orelse, .. }) = stmt else {
|
||||
let Stmt::For(for_stmt) = stmt else {
|
||||
return None;
|
||||
};
|
||||
let ast::StmtFor { orelse, .. } = &**for_stmt;
|
||||
|
||||
// The loop itself shouldn't have an `else` block.
|
||||
if !orelse.is_empty() {
|
||||
|
||||
@@ -146,7 +146,7 @@ fn reverse_comparison(expr: &Expr, locator: &Locator, stylist: &Stylist) -> Resu
|
||||
let left = (*comparison.left).clone();
|
||||
|
||||
// Copy the right side to the left side.
|
||||
*comparison.left = comparison.comparisons[0].comparator.clone();
|
||||
comparison.left = Box::new(comparison.comparisons[0].comparator.clone());
|
||||
|
||||
// Copy the left side to the right side.
|
||||
comparison.comparisons[0].comparator = left;
|
||||
|
||||
@@ -4,7 +4,8 @@ use ruff_python_ast::{self as ast, Expr, Stmt};
|
||||
pub(super) fn has_slots(body: &[Stmt]) -> bool {
|
||||
for stmt in body {
|
||||
match stmt {
|
||||
Stmt::Assign(ast::StmtAssign { targets, .. }) => {
|
||||
Stmt::Assign(assign) => {
|
||||
let targets = &assign.targets;
|
||||
for target in targets {
|
||||
if let Expr::Name(ast::ExprName { id, .. }) = target {
|
||||
if id.as_str() == "__slots__" {
|
||||
@@ -13,7 +14,8 @@ pub(super) fn has_slots(body: &[Stmt]) -> bool {
|
||||
}
|
||||
}
|
||||
}
|
||||
Stmt::AnnAssign(ast::StmtAnnAssign { target, .. }) => {
|
||||
Stmt::AnnAssign(ann_assign) => {
|
||||
let target = &ann_assign.target;
|
||||
if let Expr::Name(ast::ExprName { id, .. }) = target.as_ref() {
|
||||
if id.as_str() == "__slots__" {
|
||||
return true;
|
||||
|
||||
@@ -92,9 +92,11 @@ impl<'a> BannedModuleImportPolicies<'a> {
|
||||
pub(crate) fn new(stmt: &'a Stmt, checker: &Checker) -> Self {
|
||||
match stmt {
|
||||
Stmt::Import(import) => Self::Import(import),
|
||||
Stmt::ImportFrom(import @ StmtImportFrom { module, level, .. }) => {
|
||||
Stmt::ImportFrom(import) => {
|
||||
let module = &import.module;
|
||||
let level = import.level;
|
||||
let module = resolve_imported_module_path(
|
||||
*level,
|
||||
level,
|
||||
module.as_deref(),
|
||||
checker.module.qualified_name(),
|
||||
);
|
||||
|
||||
@@ -91,9 +91,10 @@ fn fix_banned_relative_import(
|
||||
return None;
|
||||
}
|
||||
|
||||
let Stmt::ImportFrom(ast::StmtImportFrom { names, .. }) = stmt else {
|
||||
let Stmt::ImportFrom(import_from) = stmt else {
|
||||
panic!("Expected Stmt::ImportFrom");
|
||||
};
|
||||
let names = &import_from.names;
|
||||
let node = ast::StmtImportFrom {
|
||||
module: Some(Identifier::new(
|
||||
module_path.to_string(),
|
||||
|
||||
@@ -158,10 +158,10 @@ pub(crate) fn unquoted_type_alias(checker: &Checker, binding: &Binding) {
|
||||
return;
|
||||
}
|
||||
|
||||
let Some(Stmt::AnnAssign(ast::StmtAnnAssign {
|
||||
value: Some(expr), ..
|
||||
})) = binding.statement(checker.semantic())
|
||||
else {
|
||||
let Some(Stmt::AnnAssign(node)) = binding.statement(checker.semantic()) else {
|
||||
return;
|
||||
};
|
||||
let Some(expr) = &node.value else {
|
||||
return;
|
||||
};
|
||||
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
use ruff_python_ast as ast;
|
||||
use ruff_python_ast::{Parameter, Parameters, Stmt, StmtExpr, StmtFunctionDef, StmtRaise};
|
||||
use ruff_python_ast::{Parameter, Parameters, Stmt, StmtExpr, StmtFunctionDef};
|
||||
|
||||
use ruff_macros::{ViolationMetadata, derive_message_formats};
|
||||
use ruff_python_semantic::analyze::{function_type, visibility};
|
||||
@@ -389,14 +389,20 @@ pub(crate) fn is_not_implemented_stub_with_variable(
|
||||
_ => &function_def.body,
|
||||
};
|
||||
|
||||
let [
|
||||
Stmt::Assign(ast::StmtAssign { targets, value, .. }),
|
||||
Stmt::Raise(StmtRaise {
|
||||
exc: Some(exception),
|
||||
..
|
||||
}),
|
||||
] = statements
|
||||
else {
|
||||
let [stmt1, stmt2] = statements else {
|
||||
return false;
|
||||
};
|
||||
|
||||
let Stmt::Assign(assign_node) = stmt1 else {
|
||||
return false;
|
||||
};
|
||||
let targets = &assign_node.targets;
|
||||
let value = &assign_node.value;
|
||||
|
||||
let Stmt::Raise(raise_node) = stmt2 else {
|
||||
return false;
|
||||
};
|
||||
let Some(exception) = &raise_node.exc else {
|
||||
return false;
|
||||
};
|
||||
|
||||
|
||||
@@ -23,11 +23,13 @@ pub(crate) fn annotate_imports<'a>(
|
||||
.iter()
|
||||
.map(|import| {
|
||||
match import {
|
||||
Stmt::Import(ast::StmtImport {
|
||||
names,
|
||||
range,
|
||||
node_index: _,
|
||||
}) => {
|
||||
Stmt::Import(import_stmt) => {
|
||||
let ast::StmtImport {
|
||||
names,
|
||||
range,
|
||||
node_index: _,
|
||||
} = &**import_stmt;
|
||||
|
||||
// Find comments above.
|
||||
let mut atop = vec![];
|
||||
while let Some(comment) =
|
||||
@@ -58,13 +60,15 @@ pub(crate) fn annotate_imports<'a>(
|
||||
inline,
|
||||
}
|
||||
}
|
||||
Stmt::ImportFrom(ast::StmtImportFrom {
|
||||
module,
|
||||
names,
|
||||
level,
|
||||
range: _,
|
||||
node_index: _,
|
||||
}) => {
|
||||
Stmt::ImportFrom(import_from_stmt) => {
|
||||
let ast::StmtImportFrom {
|
||||
module,
|
||||
names,
|
||||
level,
|
||||
range: _,
|
||||
node_index: _,
|
||||
} = &**import_from_stmt;
|
||||
|
||||
// Find comments above.
|
||||
let mut atop = vec![];
|
||||
while let Some(comment) =
|
||||
|
||||
@@ -183,87 +183,77 @@ impl<'a> StatementVisitor<'a> for BlockBuilder<'a> {
|
||||
let prev_nested = self.nested;
|
||||
self.nested = true;
|
||||
match stmt {
|
||||
Stmt::FunctionDef(ast::StmtFunctionDef { body, .. }) => {
|
||||
for stmt in body {
|
||||
Stmt::FunctionDef(node) => {
|
||||
for stmt in &node.body {
|
||||
self.visit_stmt(stmt);
|
||||
}
|
||||
self.finalize(None);
|
||||
}
|
||||
Stmt::ClassDef(ast::StmtClassDef { body, .. }) => {
|
||||
for stmt in body {
|
||||
Stmt::ClassDef(node) => {
|
||||
for stmt in &node.body {
|
||||
self.visit_stmt(stmt);
|
||||
}
|
||||
self.finalize(None);
|
||||
}
|
||||
Stmt::For(ast::StmtFor { body, orelse, .. }) => {
|
||||
for stmt in body {
|
||||
Stmt::For(node) => {
|
||||
for stmt in &node.body {
|
||||
self.visit_stmt(stmt);
|
||||
}
|
||||
self.finalize(None);
|
||||
|
||||
for stmt in orelse {
|
||||
for stmt in &node.orelse {
|
||||
self.visit_stmt(stmt);
|
||||
}
|
||||
self.finalize(None);
|
||||
}
|
||||
Stmt::While(ast::StmtWhile { body, orelse, .. }) => {
|
||||
for stmt in body {
|
||||
Stmt::While(node) => {
|
||||
for stmt in &node.body {
|
||||
self.visit_stmt(stmt);
|
||||
}
|
||||
self.finalize(None);
|
||||
|
||||
for stmt in orelse {
|
||||
for stmt in &node.orelse {
|
||||
self.visit_stmt(stmt);
|
||||
}
|
||||
self.finalize(None);
|
||||
}
|
||||
Stmt::If(ast::StmtIf {
|
||||
body,
|
||||
elif_else_clauses,
|
||||
..
|
||||
}) => {
|
||||
for stmt in body {
|
||||
Stmt::If(node) => {
|
||||
for stmt in &node.body {
|
||||
self.visit_stmt(stmt);
|
||||
}
|
||||
self.finalize(None);
|
||||
|
||||
for clause in elif_else_clauses {
|
||||
for clause in &node.elif_else_clauses {
|
||||
self.visit_elif_else_clause(clause);
|
||||
}
|
||||
}
|
||||
Stmt::With(ast::StmtWith { body, .. }) => {
|
||||
for stmt in body {
|
||||
Stmt::With(node) => {
|
||||
for stmt in &node.body {
|
||||
self.visit_stmt(stmt);
|
||||
}
|
||||
self.finalize(None);
|
||||
}
|
||||
Stmt::Match(ast::StmtMatch { cases, .. }) => {
|
||||
for match_case in cases {
|
||||
Stmt::Match(node) => {
|
||||
for match_case in &node.cases {
|
||||
self.visit_match_case(match_case);
|
||||
}
|
||||
}
|
||||
Stmt::Try(ast::StmtTry {
|
||||
body,
|
||||
handlers,
|
||||
orelse,
|
||||
finalbody,
|
||||
..
|
||||
}) => {
|
||||
for except_handler in handlers {
|
||||
Stmt::Try(node) => {
|
||||
for except_handler in &node.handlers {
|
||||
self.visit_except_handler(except_handler);
|
||||
}
|
||||
|
||||
for stmt in body {
|
||||
for stmt in &node.body {
|
||||
self.visit_stmt(stmt);
|
||||
}
|
||||
self.finalize(None);
|
||||
|
||||
for stmt in orelse {
|
||||
for stmt in &node.orelse {
|
||||
self.visit_stmt(stmt);
|
||||
}
|
||||
self.finalize(None);
|
||||
|
||||
for stmt in finalbody {
|
||||
for stmt in &node.finalbody {
|
||||
self.visit_stmt(stmt);
|
||||
}
|
||||
self.finalize(None);
|
||||
|
||||
@@ -59,30 +59,30 @@ impl AlwaysFixableViolation for MissingRequiredImport {
|
||||
fn includes_import(stmt: &Stmt, target: &NameImport) -> bool {
|
||||
match target {
|
||||
NameImport::Import(target) => {
|
||||
let Stmt::Import(ast::StmtImport {
|
||||
let Stmt::Import(import_stmt) = &stmt else {
|
||||
return false;
|
||||
};
|
||||
let ast::StmtImport {
|
||||
names,
|
||||
range: _,
|
||||
node_index: _,
|
||||
}) = &stmt
|
||||
else {
|
||||
return false;
|
||||
};
|
||||
} = &**import_stmt;
|
||||
names.iter().any(|alias| {
|
||||
alias.name == target.name.name
|
||||
&& alias.asname.as_deref() == target.name.as_name.as_deref()
|
||||
})
|
||||
}
|
||||
NameImport::ImportFrom(target) => {
|
||||
let Stmt::ImportFrom(ast::StmtImportFrom {
|
||||
let Stmt::ImportFrom(import_from_stmt) = &stmt else {
|
||||
return false;
|
||||
};
|
||||
let ast::StmtImportFrom {
|
||||
module,
|
||||
names,
|
||||
level,
|
||||
range: _,
|
||||
node_index: _,
|
||||
}) = &stmt
|
||||
else {
|
||||
return false;
|
||||
};
|
||||
} = &**import_from_stmt;
|
||||
module.as_deref() == target.module.as_deref()
|
||||
&& *level == target.level
|
||||
&& names.iter().any(|alias| {
|
||||
|
||||
@@ -71,39 +71,35 @@ fn get_complexity_number(stmts: &[Stmt]) -> usize {
|
||||
let mut complexity = 0;
|
||||
for stmt in stmts {
|
||||
match stmt {
|
||||
Stmt::If(ast::StmtIf {
|
||||
body,
|
||||
elif_else_clauses,
|
||||
..
|
||||
}) => {
|
||||
Stmt::If(if_stmt) => {
|
||||
complexity += 1;
|
||||
complexity += get_complexity_number(body);
|
||||
for clause in elif_else_clauses {
|
||||
complexity += get_complexity_number(&if_stmt.body);
|
||||
for clause in &if_stmt.elif_else_clauses {
|
||||
if clause.test.is_some() {
|
||||
complexity += 1;
|
||||
}
|
||||
complexity += get_complexity_number(&clause.body);
|
||||
}
|
||||
}
|
||||
Stmt::For(ast::StmtFor { body, orelse, .. }) => {
|
||||
Stmt::For(for_stmt) => {
|
||||
complexity += 1;
|
||||
complexity += get_complexity_number(body);
|
||||
complexity += get_complexity_number(orelse);
|
||||
complexity += get_complexity_number(&for_stmt.body);
|
||||
complexity += get_complexity_number(&for_stmt.orelse);
|
||||
}
|
||||
Stmt::With(ast::StmtWith { body, .. }) => {
|
||||
complexity += get_complexity_number(body);
|
||||
Stmt::With(with_stmt) => {
|
||||
complexity += get_complexity_number(&with_stmt.body);
|
||||
}
|
||||
Stmt::While(ast::StmtWhile { body, orelse, .. }) => {
|
||||
Stmt::While(while_stmt) => {
|
||||
complexity += 1;
|
||||
complexity += get_complexity_number(body);
|
||||
complexity += get_complexity_number(orelse);
|
||||
complexity += get_complexity_number(&while_stmt.body);
|
||||
complexity += get_complexity_number(&while_stmt.orelse);
|
||||
}
|
||||
Stmt::Match(ast::StmtMatch { cases, .. }) => {
|
||||
for case in cases {
|
||||
Stmt::Match(match_stmt) => {
|
||||
for case in &match_stmt.cases {
|
||||
complexity += 1;
|
||||
complexity += get_complexity_number(&case.body);
|
||||
}
|
||||
if let Some(last_case) = cases.last() {
|
||||
if let Some(last_case) = match_stmt.cases.last() {
|
||||
// The complexity of an irrefutable pattern is similar to an `else` block of an `if` statement.
|
||||
//
|
||||
// For example:
|
||||
@@ -121,20 +117,14 @@ fn get_complexity_number(stmts: &[Stmt]) -> usize {
|
||||
}
|
||||
}
|
||||
}
|
||||
Stmt::Try(ast::StmtTry {
|
||||
body,
|
||||
handlers,
|
||||
orelse,
|
||||
finalbody,
|
||||
..
|
||||
}) => {
|
||||
complexity += get_complexity_number(body);
|
||||
if !orelse.is_empty() {
|
||||
Stmt::Try(try_stmt) => {
|
||||
complexity += get_complexity_number(&try_stmt.body);
|
||||
if !try_stmt.orelse.is_empty() {
|
||||
complexity += 1;
|
||||
}
|
||||
complexity += get_complexity_number(orelse);
|
||||
complexity += get_complexity_number(finalbody);
|
||||
for handler in handlers {
|
||||
complexity += get_complexity_number(&try_stmt.orelse);
|
||||
complexity += get_complexity_number(&try_stmt.finalbody);
|
||||
for handler in &try_stmt.handlers {
|
||||
complexity += 1;
|
||||
let ExceptHandler::ExceptHandler(ast::ExceptHandlerExceptHandler {
|
||||
body, ..
|
||||
@@ -142,12 +132,12 @@ fn get_complexity_number(stmts: &[Stmt]) -> usize {
|
||||
complexity += get_complexity_number(body);
|
||||
}
|
||||
}
|
||||
Stmt::FunctionDef(ast::StmtFunctionDef { body, .. }) => {
|
||||
Stmt::FunctionDef(func_def) => {
|
||||
complexity += 1;
|
||||
complexity += get_complexity_number(body);
|
||||
complexity += get_complexity_number(&func_def.body);
|
||||
}
|
||||
Stmt::ClassDef(ast::StmtClassDef { body, .. }) => {
|
||||
complexity += get_complexity_number(body);
|
||||
Stmt::ClassDef(class_def) => {
|
||||
complexity += get_complexity_number(&class_def.body);
|
||||
}
|
||||
_ => {}
|
||||
}
|
||||
|
||||
@@ -3,7 +3,7 @@ use ruff_python_ast::name::QualifiedName;
|
||||
use ruff_python_ast::statement_visitor::StatementVisitor;
|
||||
use ruff_python_ast::visitor::Visitor;
|
||||
use ruff_python_ast::visitor::{walk_expr, walk_stmt};
|
||||
use ruff_python_ast::{Alias, Stmt, StmtImportFrom, statement_visitor};
|
||||
use ruff_python_ast::{Alias, Stmt, statement_visitor};
|
||||
use ruff_python_semantic::SemanticModel;
|
||||
|
||||
/// AST visitor that searches an AST tree for [`ast::StmtImportFrom`] nodes
|
||||
@@ -28,7 +28,9 @@ impl StatementVisitor<'_> for ImportSearcher<'_> {
|
||||
if self.found_import {
|
||||
return;
|
||||
}
|
||||
if let Stmt::ImportFrom(StmtImportFrom { module, names, .. }) = stmt {
|
||||
if let Stmt::ImportFrom(import_from) = stmt {
|
||||
let module = &import_from.module;
|
||||
let names = &import_from.names;
|
||||
if module.as_ref().is_some_and(|module| module == self.module)
|
||||
&& names.iter().any(|Alias { name, .. }| name == self.name)
|
||||
{
|
||||
|
||||
@@ -25,10 +25,10 @@ pub(super) fn is_acronym(name: &str, asname: &str) -> bool {
|
||||
|
||||
/// Returns `true` if the statement is an assignment to a named tuple.
|
||||
pub(super) fn is_named_tuple_assignment(stmt: &Stmt, semantic: &SemanticModel) -> bool {
|
||||
let Stmt::Assign(ast::StmtAssign { value, .. }) = stmt else {
|
||||
let Stmt::Assign(node) = stmt else {
|
||||
return false;
|
||||
};
|
||||
let Expr::Call(ast::ExprCall { func, .. }) = value.as_ref() else {
|
||||
let Expr::Call(ast::ExprCall { func, .. }) = node.value.as_ref() else {
|
||||
return false;
|
||||
};
|
||||
semantic
|
||||
@@ -45,10 +45,10 @@ pub(super) fn is_typed_dict_assignment(stmt: &Stmt, semantic: &SemanticModel) ->
|
||||
return false;
|
||||
}
|
||||
|
||||
let Stmt::Assign(ast::StmtAssign { value, .. }) = stmt else {
|
||||
let Stmt::Assign(node) = stmt else {
|
||||
return false;
|
||||
};
|
||||
let Expr::Call(ast::ExprCall { func, .. }) = value.as_ref() else {
|
||||
let Expr::Call(ast::ExprCall { func, .. }) = node.value.as_ref() else {
|
||||
return false;
|
||||
};
|
||||
semantic.match_typing_expr(func, "TypedDict")
|
||||
@@ -60,10 +60,10 @@ pub(super) fn is_type_var_assignment(stmt: &Stmt, semantic: &SemanticModel) -> b
|
||||
return false;
|
||||
}
|
||||
|
||||
let Stmt::Assign(ast::StmtAssign { value, .. }) = stmt else {
|
||||
let Stmt::Assign(node) = stmt else {
|
||||
return false;
|
||||
};
|
||||
let Expr::Call(ast::ExprCall { func, .. }) = value.as_ref() else {
|
||||
let Expr::Call(ast::ExprCall { func, .. }) = node.value.as_ref() else {
|
||||
return false;
|
||||
};
|
||||
semantic
|
||||
@@ -77,8 +77,8 @@ pub(super) fn is_type_var_assignment(stmt: &Stmt, semantic: &SemanticModel) -> b
|
||||
/// Returns `true` if the statement is an assignment to a `TypeAlias`.
|
||||
pub(super) fn is_type_alias_assignment(stmt: &Stmt, semantic: &SemanticModel) -> bool {
|
||||
match stmt {
|
||||
Stmt::AnnAssign(ast::StmtAnnAssign { annotation, .. }) => {
|
||||
semantic.match_typing_expr(annotation, "TypeAlias")
|
||||
Stmt::AnnAssign(node) => {
|
||||
semantic.match_typing_expr(&node.annotation, "TypeAlias")
|
||||
}
|
||||
Stmt::TypeAlias(_) => true,
|
||||
_ => false,
|
||||
@@ -157,11 +157,15 @@ pub(super) fn is_django_model_import(name: &str, stmt: &Stmt, semantic: &Semanti
|
||||
}
|
||||
|
||||
match stmt {
|
||||
Stmt::AnnAssign(ast::StmtAnnAssign {
|
||||
value: Some(value), ..
|
||||
}) => match_model_import(name, value.as_ref(), semantic),
|
||||
Stmt::Assign(ast::StmtAssign { value, .. }) => {
|
||||
match_model_import(name, value.as_ref(), semantic)
|
||||
Stmt::AnnAssign(node) => {
|
||||
if let Some(value) = &node.value {
|
||||
match_model_import(name, value.as_ref(), semantic)
|
||||
} else {
|
||||
false
|
||||
}
|
||||
}
|
||||
Stmt::Assign(node) => {
|
||||
match_model_import(name, node.value.as_ref(), semantic)
|
||||
}
|
||||
_ => false,
|
||||
}
|
||||
|
||||
@@ -92,23 +92,16 @@ pub(crate) fn manual_dict_comprehension(checker: &Checker, for_stmt: &ast::StmtF
|
||||
// if idx % 2 == 0:
|
||||
// result[name] = idx
|
||||
// ```
|
||||
[
|
||||
Stmt::If(ast::StmtIf {
|
||||
body,
|
||||
elif_else_clauses,
|
||||
test,
|
||||
..
|
||||
}),
|
||||
] => {
|
||||
[Stmt::If(node)] => {
|
||||
// TODO(charlie): If there's an `else` clause, verify that the `else` has the
|
||||
// same structure.
|
||||
if !elif_else_clauses.is_empty() {
|
||||
if !node.elif_else_clauses.is_empty() {
|
||||
return;
|
||||
}
|
||||
let [stmt] = body.as_slice() else {
|
||||
let [stmt] = node.body.as_slice() else {
|
||||
return;
|
||||
};
|
||||
(stmt, Some(test))
|
||||
(stmt, Some(&node.test))
|
||||
}
|
||||
// ```python
|
||||
// for idx, name in enumerate(names):
|
||||
@@ -118,15 +111,12 @@ pub(crate) fn manual_dict_comprehension(checker: &Checker, for_stmt: &ast::StmtF
|
||||
_ => return,
|
||||
};
|
||||
|
||||
let Stmt::Assign(ast::StmtAssign {
|
||||
targets,
|
||||
value,
|
||||
range,
|
||||
node_index: _,
|
||||
}) = stmt
|
||||
else {
|
||||
let Stmt::Assign(node) = stmt else {
|
||||
return;
|
||||
};
|
||||
let targets = &node.targets;
|
||||
let value = &node.value;
|
||||
let range = &node.range;
|
||||
|
||||
let [
|
||||
Expr::Subscript(ast::ExprSubscript {
|
||||
@@ -212,8 +202,8 @@ pub(crate) fn manual_dict_comprehension(checker: &Checker, for_stmt: &ast::StmtF
|
||||
if is_fix_manual_dict_comprehension_enabled(checker.settings()) {
|
||||
let binding_stmt = binding.statement(checker.semantic());
|
||||
let binding_value = binding_stmt.and_then(|binding_stmt| match binding_stmt {
|
||||
ast::Stmt::AnnAssign(assign) => assign.value.as_deref(),
|
||||
ast::Stmt::Assign(assign) => Some(&assign.value),
|
||||
ast::Stmt::AnnAssign(node) => node.value.as_deref(),
|
||||
ast::Stmt::Assign(node) => Some(&node.value),
|
||||
_ => None,
|
||||
});
|
||||
|
||||
@@ -243,7 +233,7 @@ pub(crate) fn manual_dict_comprehension(checker: &Checker, for_stmt: &ast::StmtF
|
||||
// but not necessarily, so this needs to be manually fixed. This does not apply when using an update.
|
||||
let binding_has_one_target = binding_stmt.is_some_and(|binding_stmt| match binding_stmt {
|
||||
ast::Stmt::AnnAssign(_) => true,
|
||||
ast::Stmt::Assign(assign) => assign.targets.len() == 1,
|
||||
ast::Stmt::Assign(node) => node.targets.len() == 1,
|
||||
_ => false,
|
||||
});
|
||||
// If the binding gets used in between the assignment and the for loop, a comprehension is no longer safe
|
||||
|
||||
@@ -109,21 +109,14 @@ pub(crate) fn manual_list_comprehension(checker: &Checker, for_stmt: &ast::StmtF
|
||||
// if z:
|
||||
// filtered.append(x)
|
||||
// ```
|
||||
[
|
||||
ast::Stmt::If(ast::StmtIf {
|
||||
body,
|
||||
elif_else_clauses,
|
||||
test,
|
||||
..
|
||||
}),
|
||||
] => {
|
||||
if !elif_else_clauses.is_empty() {
|
||||
[ast::Stmt::If(node)] => {
|
||||
if !node.elif_else_clauses.is_empty() {
|
||||
return;
|
||||
}
|
||||
let [stmt] = body.as_slice() else {
|
||||
let [stmt] = node.body.as_slice() else {
|
||||
return;
|
||||
};
|
||||
(stmt, Some(test))
|
||||
(stmt, Some(&node.test))
|
||||
}
|
||||
// ```python
|
||||
// for x in y:
|
||||
@@ -267,8 +260,8 @@ pub(crate) fn manual_list_comprehension(checker: &Checker, for_stmt: &ast::StmtF
|
||||
|
||||
let list_binding_stmt = list_binding.statement(checker.semantic());
|
||||
let list_binding_value = list_binding_stmt.and_then(|binding_stmt| match binding_stmt {
|
||||
ast::Stmt::AnnAssign(assign) => assign.value.as_deref(),
|
||||
ast::Stmt::Assign(assign) => Some(&assign.value),
|
||||
ast::Stmt::AnnAssign(node) => node.value.as_deref(),
|
||||
ast::Stmt::Assign(node) => Some(&node.value),
|
||||
_ => None,
|
||||
});
|
||||
|
||||
@@ -304,7 +297,7 @@ pub(crate) fn manual_list_comprehension(checker: &Checker, for_stmt: &ast::StmtF
|
||||
// but not necessarily, so this needs to be manually fixed. This does not apply when using an extend.
|
||||
let binding_has_one_target = list_binding_stmt.is_some_and(|binding_stmt| match binding_stmt {
|
||||
ast::Stmt::AnnAssign(_) => true,
|
||||
ast::Stmt::Assign(assign) => assign.targets.len() == 1,
|
||||
ast::Stmt::Assign(node) => node.targets.len() == 1,
|
||||
_ => false,
|
||||
});
|
||||
|
||||
@@ -464,8 +457,8 @@ fn convert_to_list_extend(
|
||||
let binding_stmt = binding.statement(semantic);
|
||||
let binding_stmt_range = binding_stmt
|
||||
.and_then(|stmt| match stmt {
|
||||
ast::Stmt::AnnAssign(assign) => Some(assign.range),
|
||||
ast::Stmt::Assign(assign) => Some(assign.range),
|
||||
ast::Stmt::AnnAssign(node) => Some(node.range),
|
||||
ast::Stmt::Assign(node) => Some(node.range),
|
||||
_ => None,
|
||||
})
|
||||
.ok_or(anyhow!(
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
use ruff_macros::{ViolationMetadata, derive_message_formats};
|
||||
use ruff_python_ast::statement_visitor::{StatementVisitor, walk_stmt};
|
||||
use ruff_python_ast::{self as ast, PythonVersion, Stmt};
|
||||
use ruff_python_ast::{PythonVersion, Stmt};
|
||||
use ruff_text_size::Ranged;
|
||||
|
||||
use crate::Violation;
|
||||
@@ -93,9 +93,11 @@ pub(crate) fn try_except_in_loop(checker: &Checker, body: &[Stmt]) {
|
||||
return;
|
||||
}
|
||||
|
||||
let [Stmt::Try(ast::StmtTry { handlers, body, .. })] = body else {
|
||||
let [Stmt::Try(try_stmt)] = body else {
|
||||
return;
|
||||
};
|
||||
let handlers = &try_stmt.handlers;
|
||||
let body = &try_stmt.body;
|
||||
|
||||
let Some(handler) = handlers.first() else {
|
||||
return;
|
||||
|
||||
@@ -207,7 +207,7 @@ fn match_mutation(stmt: &Stmt, id: &str) -> bool {
|
||||
target_id == id
|
||||
}
|
||||
// Ex) `foo[0] = bar`
|
||||
Stmt::Assign(ast::StmtAssign { targets, .. }) => targets.iter().any(|target| {
|
||||
Stmt::Assign(node) => node.targets.iter().any(|target| {
|
||||
if let Some(ast::ExprSubscript { value: target, .. }) = target.as_subscript_expr() {
|
||||
if let Some(ast::ExprName { id: target_id, .. }) = target.as_name_expr() {
|
||||
return target_id == id;
|
||||
@@ -216,16 +216,16 @@ fn match_mutation(stmt: &Stmt, id: &str) -> bool {
|
||||
false
|
||||
}),
|
||||
// Ex) `foo += bar`
|
||||
Stmt::AugAssign(ast::StmtAugAssign { target, .. }) => {
|
||||
if let Some(ast::ExprName { id: target_id, .. }) = target.as_name_expr() {
|
||||
Stmt::AugAssign(node) => {
|
||||
if let Some(ast::ExprName { id: target_id, .. }) = node.target.as_name_expr() {
|
||||
target_id == id
|
||||
} else {
|
||||
false
|
||||
}
|
||||
}
|
||||
// Ex) `foo[0]: int = bar`
|
||||
Stmt::AnnAssign(ast::StmtAnnAssign { target, .. }) => {
|
||||
if let Some(ast::ExprSubscript { value: target, .. }) = target.as_subscript_expr() {
|
||||
Stmt::AnnAssign(node) => {
|
||||
if let Some(ast::ExprSubscript { value: target, .. }) = node.target.as_subscript_expr() {
|
||||
if let Some(ast::ExprName { id: target_id, .. }) = target.as_name_expr() {
|
||||
return target_id == id;
|
||||
}
|
||||
@@ -233,7 +233,7 @@ fn match_mutation(stmt: &Stmt, id: &str) -> bool {
|
||||
false
|
||||
}
|
||||
// Ex) `del foo[0]`
|
||||
Stmt::Delete(ast::StmtDelete { targets, .. }) => targets.iter().any(|target| {
|
||||
Stmt::Delete(node) => node.targets.iter().any(|target| {
|
||||
if let Some(ast::ExprSubscript { value: target, .. }) = target.as_subscript_expr() {
|
||||
if let Some(ast::ExprName { id: target_id, .. }) = target.as_name_expr() {
|
||||
return target_id == id;
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
use ruff_macros::{ViolationMetadata, derive_message_formats};
|
||||
use ruff_python_ast::identifier::except;
|
||||
use ruff_python_ast::{self as ast, ExceptHandler, Expr, Stmt};
|
||||
use ruff_python_ast::{ExceptHandler, Expr, Stmt};
|
||||
|
||||
use crate::Violation;
|
||||
use crate::checkers::ast::Checker;
|
||||
@@ -65,7 +65,7 @@ pub(crate) fn bare_except(
|
||||
if type_.is_none()
|
||||
&& !body
|
||||
.iter()
|
||||
.any(|stmt| matches!(stmt, Stmt::Raise(ast::StmtRaise { exc: None, .. })))
|
||||
.any(|stmt| matches!(stmt, Stmt::Raise(raise_stmt) if raise_stmt.exc.is_none()))
|
||||
{
|
||||
checker.report_diagnostic(BareExcept, except(handler, checker.locator().contents()));
|
||||
}
|
||||
|
||||
@@ -223,7 +223,7 @@ fn function(
|
||||
..parameter.clone()
|
||||
})
|
||||
.collect::<Vec<_>>();
|
||||
let func = Stmt::FunctionDef(ast::StmtFunctionDef {
|
||||
let func = Stmt::FunctionDef(Box::new(ast::StmtFunctionDef {
|
||||
is_async: false,
|
||||
name: Identifier::new(name.to_string(), TextRange::default()),
|
||||
parameters: Box::new(Parameters {
|
||||
@@ -237,13 +237,13 @@ fn function(
|
||||
type_params: None,
|
||||
range: TextRange::default(),
|
||||
node_index: ruff_python_ast::AtomicNodeIndex::NONE,
|
||||
});
|
||||
}));
|
||||
let generated = checker.generator().stmt(&func);
|
||||
|
||||
return replace_trailing_ellipsis_with_original_expr(generated, lambda, stmt, checker);
|
||||
}
|
||||
}
|
||||
let function = Stmt::FunctionDef(ast::StmtFunctionDef {
|
||||
let function = Stmt::FunctionDef(Box::new(ast::StmtFunctionDef {
|
||||
is_async: false,
|
||||
name: Identifier::new(name.to_string(), TextRange::default()),
|
||||
parameters: Box::new(parameters),
|
||||
@@ -253,7 +253,7 @@ fn function(
|
||||
type_params: None,
|
||||
range: TextRange::default(),
|
||||
node_index: ruff_python_ast::AtomicNodeIndex::NONE,
|
||||
});
|
||||
}));
|
||||
let generated = checker.generator().stmt(&function);
|
||||
|
||||
replace_trailing_ellipsis_with_original_expr(generated, lambda, stmt, checker)
|
||||
|
||||
@@ -942,8 +942,8 @@ impl<'a> Visitor<'a> for BodyVisitor<'a> {
|
||||
|
||||
fn visit_stmt(&mut self, stmt: &'a Stmt) {
|
||||
match stmt {
|
||||
Stmt::Raise(ast::StmtRaise { exc, .. }) => {
|
||||
if let Some(exc) = exc.as_ref() {
|
||||
Stmt::Raise(node) => {
|
||||
if let Some(exc) = node.exc.as_ref() {
|
||||
// First try to resolve the exception directly
|
||||
if let Some(qualified_name) =
|
||||
self.semantic.resolve_qualified_name(map_callable(exc))
|
||||
|
||||
@@ -300,9 +300,9 @@ fn find_dunder_all_exprs<'a>(semantic: &'a SemanticModel) -> Vec<&'a ast::Expr>
|
||||
_ => None,
|
||||
}?;
|
||||
match stmt {
|
||||
Stmt::Assign(ast::StmtAssign { value, .. }) => Some(&**value),
|
||||
Stmt::AnnAssign(ast::StmtAnnAssign { value, .. }) => value.as_deref(),
|
||||
Stmt::AugAssign(ast::StmtAugAssign { value, .. }) => Some(&**value),
|
||||
Stmt::Assign(node) => Some(&*node.value),
|
||||
Stmt::AnnAssign(node) => node.value.as_deref(),
|
||||
Stmt::AugAssign(node) => Some(&*node.value),
|
||||
_ => None,
|
||||
}
|
||||
})
|
||||
|
||||
@@ -4,7 +4,7 @@ use ruff_macros::{ViolationMetadata, derive_message_formats};
|
||||
use ruff_python_ast::helpers::contains_effect;
|
||||
use ruff_python_ast::token::parenthesized_range;
|
||||
use ruff_python_ast::token::{TokenKind, Tokens};
|
||||
use ruff_python_ast::{self as ast, Stmt};
|
||||
use ruff_python_ast::Stmt;
|
||||
use ruff_python_semantic::Binding;
|
||||
use ruff_text_size::{Ranged, TextRange, TextSize};
|
||||
|
||||
@@ -161,14 +161,14 @@ fn remove_unused_variable(binding: &Binding, checker: &Checker) -> Option<Fix> {
|
||||
let isolation = Checker::isolation(checker.semantic().parent_statement_id(node_id));
|
||||
|
||||
// First case: simple assignment (`x = 1`)
|
||||
if let Stmt::Assign(ast::StmtAssign { targets, value, .. }) = statement {
|
||||
if let Some(target) = targets
|
||||
if let Stmt::Assign(node) = statement {
|
||||
if let Some(target) = node.targets
|
||||
.iter()
|
||||
.find(|target| binding.range() == target.range())
|
||||
{
|
||||
if target.is_name_expr() {
|
||||
return if targets.len() > 1
|
||||
|| contains_effect(value, |id| checker.semantic().has_builtin_binding(id))
|
||||
return if node.targets.len() > 1
|
||||
|| contains_effect(&node.value, |id| checker.semantic().has_builtin_binding(id))
|
||||
{
|
||||
// If the expression is complex (`x = foo()`), remove the assignment,
|
||||
// but preserve the right-hand side.
|
||||
@@ -200,35 +200,32 @@ fn remove_unused_variable(binding: &Binding, checker: &Checker) -> Option<Fix> {
|
||||
}
|
||||
|
||||
// Second case: simple annotated assignment (`x: int = 1`)
|
||||
if let Stmt::AnnAssign(ast::StmtAnnAssign {
|
||||
target,
|
||||
value: Some(value),
|
||||
..
|
||||
}) = statement
|
||||
{
|
||||
if target.is_name_expr() {
|
||||
return if contains_effect(value, |id| checker.semantic().has_builtin_binding(id)) {
|
||||
// If the expression is complex (`x = foo()`), remove the assignment,
|
||||
// but preserve the right-hand side.
|
||||
let start = statement.start();
|
||||
let end =
|
||||
match_token_after(checker.tokens(), start, |token| token == TokenKind::Equal)?
|
||||
.start();
|
||||
let edit = Edit::deletion(start, end);
|
||||
Some(Fix::unsafe_edit(edit))
|
||||
} else {
|
||||
// If (e.g.) assigning to a constant (`x = 1`), delete the entire statement.
|
||||
let edit = delete_stmt(statement, parent, checker.locator(), checker.indexer());
|
||||
Some(Fix::unsafe_edit(edit).isolate(isolation))
|
||||
};
|
||||
if let Stmt::AnnAssign(node) = statement {
|
||||
if let Some(value) = &node.value {
|
||||
if node.target.is_name_expr() {
|
||||
return if contains_effect(value, |id| checker.semantic().has_builtin_binding(id)) {
|
||||
// If the expression is complex (`x = foo()`), remove the assignment,
|
||||
// but preserve the right-hand side.
|
||||
let start = statement.start();
|
||||
let end =
|
||||
match_token_after(checker.tokens(), start, |token| token == TokenKind::Equal)?
|
||||
.start();
|
||||
let edit = Edit::deletion(start, end);
|
||||
Some(Fix::unsafe_edit(edit))
|
||||
} else {
|
||||
// If (e.g.) assigning to a constant (`x = 1`), delete the entire statement.
|
||||
let edit = delete_stmt(statement, parent, checker.locator(), checker.indexer());
|
||||
Some(Fix::unsafe_edit(edit).isolate(isolation))
|
||||
};
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Third case: with_item (`with foo() as x:`)
|
||||
if let Stmt::With(ast::StmtWith { items, .. }) = statement {
|
||||
if let Stmt::With(node) = statement {
|
||||
// Find the binding that matches the given `Range`.
|
||||
// TODO(charlie): Store the `WithItem` in the `Binding`.
|
||||
for item in items {
|
||||
for item in &node.items {
|
||||
if let Some(optional_vars) = &item.optional_vars {
|
||||
if optional_vars.range() == binding.range() {
|
||||
// Find the first token before the `as` keyword.
|
||||
|
||||
@@ -119,22 +119,22 @@ impl Visitor<'_> for SequenceIndexVisitor<'_> {
|
||||
return;
|
||||
}
|
||||
match stmt {
|
||||
Stmt::Assign(ast::StmtAssign { targets, value, .. }) => {
|
||||
self.modified = targets.iter().any(|target| self.is_assignment(target));
|
||||
self.visit_expr(value);
|
||||
Stmt::Assign(node) => {
|
||||
self.modified = node.targets.iter().any(|target| self.is_assignment(target));
|
||||
self.visit_expr(&node.value);
|
||||
}
|
||||
Stmt::AnnAssign(ast::StmtAnnAssign { target, value, .. }) => {
|
||||
if let Some(value) = value {
|
||||
self.modified = self.is_assignment(target);
|
||||
Stmt::AnnAssign(node) => {
|
||||
if let Some(value) = &node.value {
|
||||
self.modified = self.is_assignment(&node.target);
|
||||
self.visit_expr(value);
|
||||
}
|
||||
}
|
||||
Stmt::AugAssign(ast::StmtAugAssign { target, value, .. }) => {
|
||||
self.modified = self.is_assignment(target);
|
||||
self.visit_expr(value);
|
||||
Stmt::AugAssign(node) => {
|
||||
self.modified = self.is_assignment(&node.target);
|
||||
self.visit_expr(&node.value);
|
||||
}
|
||||
Stmt::Delete(ast::StmtDelete { targets, .. }) => {
|
||||
self.modified = targets.iter().any(|target| self.is_assignment(target));
|
||||
Stmt::Delete(node) => {
|
||||
self.modified = node.targets.iter().any(|target| self.is_assignment(target));
|
||||
}
|
||||
_ => visitor::walk_stmt(self, stmt),
|
||||
}
|
||||
|
||||
@@ -64,12 +64,10 @@ impl Violation for CollapsibleElseIf {
|
||||
|
||||
/// PLR5501
|
||||
pub(crate) fn collapsible_else_if(checker: &Checker, stmt: &Stmt) {
|
||||
let Stmt::If(ast::StmtIf {
|
||||
elif_else_clauses, ..
|
||||
}) = stmt
|
||||
else {
|
||||
let Stmt::If(if_stmt) = stmt else {
|
||||
return;
|
||||
};
|
||||
let elif_else_clauses = &if_stmt.elif_else_clauses;
|
||||
|
||||
let Some(
|
||||
else_clause @ ElifElseClause {
|
||||
@@ -79,7 +77,10 @@ pub(crate) fn collapsible_else_if(checker: &Checker, stmt: &Stmt) {
|
||||
else {
|
||||
return;
|
||||
};
|
||||
let [first @ Stmt::If(ast::StmtIf { .. })] = body.as_slice() else {
|
||||
let [first] = body.as_slice() else {
|
||||
return;
|
||||
};
|
||||
let Stmt::If(_) = first else {
|
||||
return;
|
||||
};
|
||||
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
use ruff_python_ast::{self as ast, Stmt};
|
||||
use ruff_python_ast::Stmt;
|
||||
|
||||
use ruff_macros::{ViolationMetadata, derive_message_formats};
|
||||
use ruff_text_size::Ranged;
|
||||
@@ -54,28 +54,27 @@ fn traverse_body(checker: &Checker, body: &[Stmt]) {
|
||||
}
|
||||
|
||||
match stmt {
|
||||
Stmt::If(ast::StmtIf {
|
||||
body,
|
||||
elif_else_clauses,
|
||||
..
|
||||
}) => {
|
||||
traverse_body(checker, body);
|
||||
for clause in elif_else_clauses {
|
||||
Stmt::If(if_stmt) => {
|
||||
traverse_body(checker, &if_stmt.body);
|
||||
for clause in &if_stmt.elif_else_clauses {
|
||||
traverse_body(checker, &clause.body);
|
||||
}
|
||||
}
|
||||
Stmt::Try(ast::StmtTry { body, orelse, .. }) => {
|
||||
traverse_body(checker, body);
|
||||
traverse_body(checker, orelse);
|
||||
Stmt::Try(try_stmt) => {
|
||||
traverse_body(checker, &try_stmt.body);
|
||||
traverse_body(checker, &try_stmt.orelse);
|
||||
}
|
||||
Stmt::For(ast::StmtFor { orelse, .. }) | Stmt::While(ast::StmtWhile { orelse, .. }) => {
|
||||
traverse_body(checker, orelse);
|
||||
Stmt::For(for_stmt) => {
|
||||
traverse_body(checker, &for_stmt.orelse);
|
||||
}
|
||||
Stmt::With(ast::StmtWith { body, .. }) => {
|
||||
traverse_body(checker, body);
|
||||
Stmt::While(while_stmt) => {
|
||||
traverse_body(checker, &while_stmt.orelse);
|
||||
}
|
||||
Stmt::Match(ast::StmtMatch { cases, .. }) => {
|
||||
for case in cases {
|
||||
Stmt::With(with_stmt) => {
|
||||
traverse_body(checker, &with_stmt.body);
|
||||
}
|
||||
Stmt::Match(match_stmt) => {
|
||||
for case in &match_stmt.cases {
|
||||
traverse_body(checker, &case.body);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
use ruff_python_ast::{self as ast, Expr, Stmt};
|
||||
use ruff_python_ast::{Expr, Stmt};
|
||||
|
||||
use ruff_macros::{ViolationMetadata, derive_message_formats};
|
||||
use ruff_python_semantic::analyze::typing::is_dict;
|
||||
@@ -109,12 +109,14 @@ fn is_dict_key_tuple_with_two_elements(binding: &Binding, semantic: &SemanticMod
|
||||
};
|
||||
|
||||
let (value, annotation) = match statement {
|
||||
Stmt::Assign(assign_stmt) => (assign_stmt.value.as_ref(), None),
|
||||
Stmt::AnnAssign(ast::StmtAnnAssign {
|
||||
value: Some(value),
|
||||
annotation,
|
||||
..
|
||||
}) => (value.as_ref(), Some(annotation.as_ref())),
|
||||
Stmt::Assign(node) => (node.value.as_ref(), None),
|
||||
Stmt::AnnAssign(node) => {
|
||||
if let Some(value) = &node.value {
|
||||
(value.as_ref(), Some(node.annotation.as_ref()))
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
_ => return false,
|
||||
};
|
||||
|
||||
|
||||
@@ -95,16 +95,11 @@ pub(crate) fn if_stmt_min_max(checker: &Checker, stmt_if: &ast::StmtIf) {
|
||||
return;
|
||||
}
|
||||
|
||||
let [
|
||||
body @ Stmt::Assign(ast::StmtAssign {
|
||||
targets: body_targets,
|
||||
value: body_value,
|
||||
..
|
||||
}),
|
||||
] = body.as_slice()
|
||||
else {
|
||||
let [body @ Stmt::Assign(node)] = body.as_slice() else {
|
||||
return;
|
||||
};
|
||||
let body_targets = &node.targets;
|
||||
let body_value = &node.value;
|
||||
let [body_target] = body_targets.as_slice() else {
|
||||
return;
|
||||
};
|
||||
|
||||
@@ -113,7 +113,9 @@ fn get_undecorated_methods(checker: &Checker, class_stmt: &Stmt, method_type: &M
|
||||
|
||||
// gather all explicit *method calls
|
||||
for stmt in &class_def.body {
|
||||
if let Stmt::Assign(ast::StmtAssign { targets, value, .. }) = stmt {
|
||||
if let Stmt::Assign(node) = stmt {
|
||||
let targets = &node.targets;
|
||||
let value = &node.value;
|
||||
if let Expr::Call(ast::ExprCall {
|
||||
func, arguments, ..
|
||||
}) = value.as_ref()
|
||||
@@ -149,12 +151,10 @@ fn get_undecorated_methods(checker: &Checker, class_stmt: &Stmt, method_type: &M
|
||||
}
|
||||
|
||||
for stmt in &class_def.body {
|
||||
if let Stmt::FunctionDef(ast::StmtFunctionDef {
|
||||
name,
|
||||
decorator_list,
|
||||
..
|
||||
}) = stmt
|
||||
{
|
||||
if let Stmt::FunctionDef(node) = stmt {
|
||||
let name = &node.name;
|
||||
let decorator_list = &node.decorator_list;
|
||||
|
||||
let Some(decorator_call_statement) = explicit_decorator_calls.get(name.id()) else {
|
||||
continue;
|
||||
};
|
||||
|
||||
@@ -107,13 +107,13 @@ fn is_attributes_not_in_slots(body: &[Stmt]) -> Vec<AttributeAssignment<'_>> {
|
||||
for statement in body {
|
||||
match statement {
|
||||
// Ex) `__slots__ = ("name",)`
|
||||
Stmt::Assign(ast::StmtAssign { targets, value, .. }) => {
|
||||
let [Expr::Name(ast::ExprName { id, .. })] = targets.as_slice() else {
|
||||
Stmt::Assign(node) => {
|
||||
let [Expr::Name(ast::ExprName { id, .. })] = node.targets.as_slice() else {
|
||||
continue;
|
||||
};
|
||||
|
||||
if id == "__slots__" {
|
||||
for attribute in slots_attributes(value) {
|
||||
for attribute in slots_attributes(&node.value) {
|
||||
if let Some(attribute) = attribute {
|
||||
slots.insert(attribute);
|
||||
} else {
|
||||
@@ -124,12 +124,12 @@ fn is_attributes_not_in_slots(body: &[Stmt]) -> Vec<AttributeAssignment<'_>> {
|
||||
}
|
||||
|
||||
// Ex) `__slots__: Tuple[str, ...] = ("name",)`
|
||||
Stmt::AnnAssign(ast::StmtAnnAssign {
|
||||
target,
|
||||
value: Some(value),
|
||||
..
|
||||
}) => {
|
||||
let Expr::Name(ast::ExprName { id, .. }) = target.as_ref() else {
|
||||
Stmt::AnnAssign(node) => {
|
||||
let Some(value) = &node.value else {
|
||||
continue;
|
||||
};
|
||||
|
||||
let Expr::Name(ast::ExprName { id, .. }) = node.target.as_ref() else {
|
||||
continue;
|
||||
};
|
||||
|
||||
@@ -145,13 +145,13 @@ fn is_attributes_not_in_slots(body: &[Stmt]) -> Vec<AttributeAssignment<'_>> {
|
||||
}
|
||||
|
||||
// Ex) `__slots__ += ("name",)`
|
||||
Stmt::AugAssign(ast::StmtAugAssign { target, value, .. }) => {
|
||||
let Expr::Name(ast::ExprName { id, .. }) = target.as_ref() else {
|
||||
Stmt::AugAssign(node) => {
|
||||
let Expr::Name(ast::ExprName { id, .. }) = node.target.as_ref() else {
|
||||
continue;
|
||||
};
|
||||
|
||||
if id == "__slots__" {
|
||||
for attribute in slots_attributes(value) {
|
||||
for attribute in slots_attributes(&node.value) {
|
||||
if let Some(attribute) = attribute {
|
||||
slots.insert(attribute);
|
||||
} else {
|
||||
@@ -170,11 +170,11 @@ fn is_attributes_not_in_slots(body: &[Stmt]) -> Vec<AttributeAssignment<'_>> {
|
||||
|
||||
// And, collect all the property name with setter.
|
||||
for statement in body {
|
||||
let Stmt::FunctionDef(ast::StmtFunctionDef { decorator_list, .. }) = statement else {
|
||||
let Stmt::FunctionDef(node) = statement else {
|
||||
continue;
|
||||
};
|
||||
|
||||
for decorator in decorator_list {
|
||||
for decorator in &node.decorator_list {
|
||||
let Some(ast::ExprAttribute { value, attr, .. }) =
|
||||
decorator.expression.as_attribute_expr()
|
||||
else {
|
||||
@@ -193,16 +193,16 @@ fn is_attributes_not_in_slots(body: &[Stmt]) -> Vec<AttributeAssignment<'_>> {
|
||||
// Second, find any assignments that aren't included in `__slots__`.
|
||||
let mut assignments = vec![];
|
||||
for statement in body {
|
||||
let Stmt::FunctionDef(ast::StmtFunctionDef { name, body, .. }) = statement else {
|
||||
let Stmt::FunctionDef(node) = statement else {
|
||||
continue;
|
||||
};
|
||||
|
||||
if name == "__init__" {
|
||||
for statement in body {
|
||||
if node.name.as_str() == "__init__" {
|
||||
for statement in &node.body {
|
||||
match statement {
|
||||
// Ex) `self.name = name`
|
||||
Stmt::Assign(ast::StmtAssign { targets, .. }) => {
|
||||
let [Expr::Attribute(attribute)] = targets.as_slice() else {
|
||||
Stmt::Assign(assign_node) => {
|
||||
let [Expr::Attribute(attribute)] = assign_node.targets.as_slice() else {
|
||||
continue;
|
||||
};
|
||||
let Expr::Name(ast::ExprName { id, .. }) = attribute.value.as_ref() else {
|
||||
@@ -217,8 +217,8 @@ fn is_attributes_not_in_slots(body: &[Stmt]) -> Vec<AttributeAssignment<'_>> {
|
||||
}
|
||||
|
||||
// Ex) `self.name: str = name`
|
||||
Stmt::AnnAssign(ast::StmtAnnAssign { target, .. }) => {
|
||||
let Expr::Attribute(attribute) = target.as_ref() else {
|
||||
Stmt::AnnAssign(ann_node) => {
|
||||
let Expr::Attribute(attribute) = ann_node.target.as_ref() else {
|
||||
continue;
|
||||
};
|
||||
let Expr::Name(ast::ExprName { id, .. }) = attribute.value.as_ref() else {
|
||||
@@ -233,8 +233,8 @@ fn is_attributes_not_in_slots(body: &[Stmt]) -> Vec<AttributeAssignment<'_>> {
|
||||
}
|
||||
|
||||
// Ex) `self.name += name`
|
||||
Stmt::AugAssign(ast::StmtAugAssign { target, .. }) => {
|
||||
let Expr::Attribute(attribute) = target.as_ref() else {
|
||||
Stmt::AugAssign(aug_node) => {
|
||||
let Expr::Attribute(attribute) = aug_node.target.as_ref() else {
|
||||
continue;
|
||||
};
|
||||
let Expr::Name(ast::ExprName { id, .. }) = attribute.value.as_ref() else {
|
||||
|
||||
@@ -149,9 +149,9 @@ impl<'b> StatementVisitor<'b> for InnerForWithAssignTargetsVisitor<'_, 'b> {
|
||||
fn visit_stmt(&mut self, stmt: &'b Stmt) {
|
||||
// Collect target expressions.
|
||||
match stmt {
|
||||
Stmt::For(ast::StmtFor { target, .. }) => {
|
||||
Stmt::For(node) => {
|
||||
self.assignment_targets.extend(
|
||||
assignment_targets_from_expr(target, self.dummy_variable_rgx).map(|expr| {
|
||||
assignment_targets_from_expr(&node.target, self.dummy_variable_rgx).map(|expr| {
|
||||
ExprWithInnerBindingKind {
|
||||
expr,
|
||||
binding_kind: InnerBindingKind::For,
|
||||
@@ -159,9 +159,9 @@ impl<'b> StatementVisitor<'b> for InnerForWithAssignTargetsVisitor<'_, 'b> {
|
||||
}),
|
||||
);
|
||||
}
|
||||
Stmt::With(ast::StmtWith { items, .. }) => {
|
||||
Stmt::With(node) => {
|
||||
self.assignment_targets.extend(
|
||||
assignment_targets_from_with_items(items, self.dummy_variable_rgx).map(
|
||||
assignment_targets_from_with_items(&node.items, self.dummy_variable_rgx).map(
|
||||
|expr| ExprWithInnerBindingKind {
|
||||
expr,
|
||||
binding_kind: InnerBindingKind::With,
|
||||
@@ -169,17 +169,18 @@ impl<'b> StatementVisitor<'b> for InnerForWithAssignTargetsVisitor<'_, 'b> {
|
||||
),
|
||||
);
|
||||
}
|
||||
Stmt::Assign(ast::StmtAssign { targets, value, .. }) => {
|
||||
Stmt::Assign(node) => {
|
||||
// Check for single-target assignments which are of the
|
||||
// form `x = cast(..., x)`.
|
||||
if targets
|
||||
if node
|
||||
.targets
|
||||
.first()
|
||||
.is_some_and(|target| assignment_is_cast_expr(value, target, self.context))
|
||||
.is_some_and(|target| assignment_is_cast_expr(&node.value, target, self.context))
|
||||
{
|
||||
return;
|
||||
}
|
||||
self.assignment_targets.extend(
|
||||
assignment_targets_from_assign_targets(targets, self.dummy_variable_rgx).map(
|
||||
assignment_targets_from_assign_targets(&node.targets, self.dummy_variable_rgx).map(
|
||||
|expr| ExprWithInnerBindingKind {
|
||||
expr,
|
||||
binding_kind: InnerBindingKind::Assignment,
|
||||
@@ -187,9 +188,9 @@ impl<'b> StatementVisitor<'b> for InnerForWithAssignTargetsVisitor<'_, 'b> {
|
||||
),
|
||||
);
|
||||
}
|
||||
Stmt::AugAssign(ast::StmtAugAssign { target, .. }) => {
|
||||
Stmt::AugAssign(node) => {
|
||||
self.assignment_targets.extend(
|
||||
assignment_targets_from_expr(target, self.dummy_variable_rgx).map(|expr| {
|
||||
assignment_targets_from_expr(&node.target, self.dummy_variable_rgx).map(|expr| {
|
||||
ExprWithInnerBindingKind {
|
||||
expr,
|
||||
binding_kind: InnerBindingKind::Assignment,
|
||||
@@ -197,12 +198,12 @@ impl<'b> StatementVisitor<'b> for InnerForWithAssignTargetsVisitor<'_, 'b> {
|
||||
}),
|
||||
);
|
||||
}
|
||||
Stmt::AnnAssign(ast::StmtAnnAssign { target, value, .. }) => {
|
||||
if value.is_none() {
|
||||
Stmt::AnnAssign(node) => {
|
||||
if node.value.is_none() {
|
||||
return;
|
||||
}
|
||||
self.assignment_targets.extend(
|
||||
assignment_targets_from_expr(target, self.dummy_variable_rgx).map(|expr| {
|
||||
assignment_targets_from_expr(&node.target, self.dummy_variable_rgx).map(|expr| {
|
||||
ExprWithInnerBindingKind {
|
||||
expr,
|
||||
binding_kind: InnerBindingKind::Assignment,
|
||||
@@ -345,9 +346,9 @@ fn assignment_targets_from_assign_targets<'a>(
|
||||
/// PLW2901
|
||||
pub(crate) fn redefined_loop_name(checker: &Checker, stmt: &Stmt) {
|
||||
let (outer_assignment_targets, inner_assignment_targets) = match stmt {
|
||||
Stmt::With(ast::StmtWith { items, body, .. }) => {
|
||||
Stmt::With(node) => {
|
||||
let outer_assignment_targets: Vec<ExprWithOuterBindingKind> =
|
||||
assignment_targets_from_with_items(items, &checker.settings().dummy_variable_rgx)
|
||||
assignment_targets_from_with_items(&node.items, &checker.settings().dummy_variable_rgx)
|
||||
.map(|expr| ExprWithOuterBindingKind {
|
||||
expr,
|
||||
binding_kind: OuterBindingKind::With,
|
||||
@@ -358,14 +359,14 @@ pub(crate) fn redefined_loop_name(checker: &Checker, stmt: &Stmt) {
|
||||
dummy_variable_rgx: &checker.settings().dummy_variable_rgx,
|
||||
assignment_targets: vec![],
|
||||
};
|
||||
for stmt in body {
|
||||
for stmt in &node.body {
|
||||
visitor.visit_stmt(stmt);
|
||||
}
|
||||
(outer_assignment_targets, visitor.assignment_targets)
|
||||
}
|
||||
Stmt::For(ast::StmtFor { target, body, .. }) => {
|
||||
Stmt::For(node) => {
|
||||
let outer_assignment_targets: Vec<ExprWithOuterBindingKind> =
|
||||
assignment_targets_from_expr(target, &checker.settings().dummy_variable_rgx)
|
||||
assignment_targets_from_expr(&node.target, &checker.settings().dummy_variable_rgx)
|
||||
.map(|expr| ExprWithOuterBindingKind {
|
||||
expr,
|
||||
binding_kind: OuterBindingKind::For,
|
||||
@@ -376,7 +377,7 @@ pub(crate) fn redefined_loop_name(checker: &Checker, stmt: &Stmt) {
|
||||
dummy_variable_rgx: &checker.settings().dummy_variable_rgx,
|
||||
assignment_targets: vec![],
|
||||
};
|
||||
for stmt in body {
|
||||
for stmt in &node.body {
|
||||
visitor.visit_stmt(stmt);
|
||||
}
|
||||
(outer_assignment_targets, visitor.assignment_targets)
|
||||
|
||||
@@ -121,23 +121,23 @@ fn slots_members(body: &[Stmt]) -> FxHashSet<Slot<'_>> {
|
||||
for stmt in body {
|
||||
match stmt {
|
||||
// Ex) `__slots__ = ("name",)`
|
||||
Stmt::Assign(ast::StmtAssign { targets, value, .. }) => {
|
||||
let [Expr::Name(ast::ExprName { id, .. })] = targets.as_slice() else {
|
||||
Stmt::Assign(node) => {
|
||||
let [Expr::Name(ast::ExprName { id, .. })] = node.targets.as_slice() else {
|
||||
continue;
|
||||
};
|
||||
|
||||
if id == "__slots__" {
|
||||
members.extend(slots_attributes(value));
|
||||
members.extend(slots_attributes(&node.value));
|
||||
}
|
||||
}
|
||||
|
||||
// Ex) `__slots__: Tuple[str, ...] = ("name",)`
|
||||
Stmt::AnnAssign(ast::StmtAnnAssign {
|
||||
target,
|
||||
value: Some(value),
|
||||
..
|
||||
}) => {
|
||||
let Expr::Name(ast::ExprName { id, .. }) = target.as_ref() else {
|
||||
Stmt::AnnAssign(node) => {
|
||||
let Some(value) = &node.value else {
|
||||
continue;
|
||||
};
|
||||
|
||||
let Expr::Name(ast::ExprName { id, .. }) = node.target.as_ref() else {
|
||||
continue;
|
||||
};
|
||||
|
||||
@@ -147,13 +147,13 @@ fn slots_members(body: &[Stmt]) -> FxHashSet<Slot<'_>> {
|
||||
}
|
||||
|
||||
// Ex) `__slots__ += ("name",)`
|
||||
Stmt::AugAssign(ast::StmtAugAssign { target, value, .. }) => {
|
||||
let Expr::Name(ast::ExprName { id, .. }) = target.as_ref() else {
|
||||
Stmt::AugAssign(node) => {
|
||||
let Expr::Name(ast::ExprName { id, .. }) = node.target.as_ref() else {
|
||||
continue;
|
||||
};
|
||||
|
||||
if id == "__slots__" {
|
||||
members.extend(slots_attributes(value));
|
||||
members.extend(slots_attributes(&node.value));
|
||||
}
|
||||
}
|
||||
_ => {}
|
||||
|
||||
@@ -62,9 +62,20 @@ impl Violation for SingleStringSlots {
|
||||
pub(crate) fn single_string_slots(checker: &Checker, class: &StmtClassDef) {
|
||||
for stmt in &class.body {
|
||||
match stmt {
|
||||
Stmt::Assign(ast::StmtAssign { targets, value, .. }) => {
|
||||
for target in targets {
|
||||
Stmt::Assign(assign) => {
|
||||
for target in &assign.targets {
|
||||
if let Expr::Name(ast::ExprName { id, .. }) = target {
|
||||
if id.as_str() == "__slots__" {
|
||||
if matches!(assign.value.as_ref(), Expr::StringLiteral(_) | Expr::FString(_)) {
|
||||
checker.report_diagnostic(SingleStringSlots, stmt.identifier());
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Stmt::AnnAssign(ann_assign) => {
|
||||
if let Some(value) = &ann_assign.value {
|
||||
if let Expr::Name(ast::ExprName { id, .. }) = ann_assign.target.as_ref() {
|
||||
if id.as_str() == "__slots__" {
|
||||
if matches!(value.as_ref(), Expr::StringLiteral(_) | Expr::FString(_)) {
|
||||
checker.report_diagnostic(SingleStringSlots, stmt.identifier());
|
||||
@@ -73,19 +84,6 @@ pub(crate) fn single_string_slots(checker: &Checker, class: &StmtClassDef) {
|
||||
}
|
||||
}
|
||||
}
|
||||
Stmt::AnnAssign(ast::StmtAnnAssign {
|
||||
target,
|
||||
value: Some(value),
|
||||
..
|
||||
}) => {
|
||||
if let Expr::Name(ast::ExprName { id, .. }) = target.as_ref() {
|
||||
if id.as_str() == "__slots__" {
|
||||
if matches!(value.as_ref(), Expr::StringLiteral(_) | Expr::FString(_)) {
|
||||
checker.report_diagnostic(SingleStringSlots, stmt.identifier());
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
_ => {}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -79,13 +79,15 @@ impl<'a> Visitor<'a> for GeneratorAnalyzer<'a, '_> {
|
||||
fn visit_stmt(&mut self, stmt: &'a ast::Stmt) {
|
||||
match stmt {
|
||||
ast::Stmt::FunctionDef(_) => {}
|
||||
ast::Stmt::Raise(raise @ ast::StmtRaise { exc: Some(exc), .. }) => {
|
||||
if self
|
||||
.checker
|
||||
.semantic()
|
||||
.match_builtin_expr(map_callable(exc), "StopIteration")
|
||||
{
|
||||
self.stop_iteration_raises.push(raise);
|
||||
ast::Stmt::Raise(raise) => {
|
||||
if let Some(exc) = &raise.exc {
|
||||
if self
|
||||
.checker
|
||||
.semantic()
|
||||
.match_builtin_expr(map_callable(exc), "StopIteration")
|
||||
{
|
||||
self.stop_iteration_raises.push(raise);
|
||||
}
|
||||
}
|
||||
walk_stmt(self, stmt);
|
||||
}
|
||||
|
||||
@@ -166,57 +166,54 @@ fn num_branches(stmts: &[Stmt]) -> usize {
|
||||
stmts
|
||||
.iter()
|
||||
.map(|stmt| match stmt {
|
||||
Stmt::If(ast::StmtIf {
|
||||
body,
|
||||
elif_else_clauses,
|
||||
..
|
||||
}) => {
|
||||
1 + num_branches(body)
|
||||
+ elif_else_clauses.len()
|
||||
+ elif_else_clauses
|
||||
Stmt::If(node) => {
|
||||
1 + num_branches(&node.body)
|
||||
+ node.elif_else_clauses.len()
|
||||
+ node.elif_else_clauses
|
||||
.iter()
|
||||
.map(|clause| num_branches(&clause.body))
|
||||
.sum::<usize>()
|
||||
}
|
||||
Stmt::Match(ast::StmtMatch { cases, .. }) => {
|
||||
cases.len()
|
||||
+ cases
|
||||
Stmt::Match(node) => {
|
||||
node.cases.len()
|
||||
+ node.cases
|
||||
.iter()
|
||||
.map(|case| num_branches(&case.body))
|
||||
.sum::<usize>()
|
||||
}
|
||||
// The `with` statement is not considered a branch but the statements inside the `with` should be counted.
|
||||
Stmt::With(ast::StmtWith { body, .. }) => num_branches(body),
|
||||
Stmt::For(ast::StmtFor { body, orelse, .. })
|
||||
| Stmt::While(ast::StmtWhile { body, orelse, .. }) => {
|
||||
1 + num_branches(body)
|
||||
+ (if orelse.is_empty() {
|
||||
Stmt::With(node) => num_branches(&node.body),
|
||||
Stmt::For(node) => {
|
||||
1 + num_branches(&node.body)
|
||||
+ (if node.orelse.is_empty() {
|
||||
0
|
||||
} else {
|
||||
1 + num_branches(orelse)
|
||||
1 + num_branches(&node.orelse)
|
||||
})
|
||||
}
|
||||
Stmt::Try(ast::StmtTry {
|
||||
body,
|
||||
handlers,
|
||||
orelse,
|
||||
finalbody,
|
||||
..
|
||||
}) => {
|
||||
Stmt::While(node) => {
|
||||
1 + num_branches(&node.body)
|
||||
+ (if node.orelse.is_empty() {
|
||||
0
|
||||
} else {
|
||||
1 + num_branches(&node.orelse)
|
||||
})
|
||||
}
|
||||
Stmt::Try(node) => {
|
||||
// Count each `except` clause as a branch; the `else` and `finally` clauses also
|
||||
// count, but the `try` clause itself does not.
|
||||
num_branches(body)
|
||||
+ (if orelse.is_empty() {
|
||||
num_branches(&node.body)
|
||||
+ (if node.orelse.is_empty() {
|
||||
0
|
||||
} else {
|
||||
1 + num_branches(orelse)
|
||||
1 + num_branches(&node.orelse)
|
||||
})
|
||||
+ (if finalbody.is_empty() {
|
||||
+ (if node.finalbody.is_empty() {
|
||||
0
|
||||
} else {
|
||||
1 + num_branches(finalbody)
|
||||
1 + num_branches(&node.finalbody)
|
||||
})
|
||||
+ handlers
|
||||
+ node.handlers
|
||||
.iter()
|
||||
.map(|handler| {
|
||||
1 + {
|
||||
|
||||
@@ -95,39 +95,29 @@ fn is_nested_block(stmt: &Stmt) -> bool {
|
||||
/// Returns `true` if the given statement is a leaf node.
|
||||
fn has_nested_block(stmt: &Stmt) -> bool {
|
||||
match stmt {
|
||||
Stmt::If(ast::StmtIf {
|
||||
body,
|
||||
elif_else_clauses,
|
||||
..
|
||||
}) => {
|
||||
body.iter().any(is_nested_block)
|
||||
|| elif_else_clauses
|
||||
Stmt::If(node) => {
|
||||
node.body.iter().any(is_nested_block)
|
||||
|| node.elif_else_clauses
|
||||
.iter()
|
||||
.any(|elif_else| elif_else.body.iter().any(is_nested_block))
|
||||
}
|
||||
Stmt::While(ast::StmtWhile { body, orelse, .. }) => {
|
||||
body.iter().any(is_nested_block) || orelse.iter().any(is_nested_block)
|
||||
Stmt::While(node) => {
|
||||
node.body.iter().any(is_nested_block) || node.orelse.iter().any(is_nested_block)
|
||||
}
|
||||
Stmt::For(ast::StmtFor { body, orelse, .. }) => {
|
||||
body.iter().any(is_nested_block) || orelse.iter().any(is_nested_block)
|
||||
Stmt::For(node) => {
|
||||
node.body.iter().any(is_nested_block) || node.orelse.iter().any(is_nested_block)
|
||||
}
|
||||
Stmt::Try(ast::StmtTry {
|
||||
body,
|
||||
handlers,
|
||||
orelse,
|
||||
finalbody,
|
||||
..
|
||||
}) => {
|
||||
body.iter().any(is_nested_block)
|
||||
|| handlers.iter().any(|handler| match handler {
|
||||
Stmt::Try(node) => {
|
||||
node.body.iter().any(is_nested_block)
|
||||
|| node.handlers.iter().any(|handler| match handler {
|
||||
ExceptHandler::ExceptHandler(ast::ExceptHandlerExceptHandler {
|
||||
body, ..
|
||||
}) => body.iter().any(is_nested_block),
|
||||
})
|
||||
|| orelse.iter().any(is_nested_block)
|
||||
|| finalbody.iter().any(is_nested_block)
|
||||
|| node.orelse.iter().any(is_nested_block)
|
||||
|| node.finalbody.iter().any(is_nested_block)
|
||||
}
|
||||
Stmt::With(ast::StmtWith { body, .. }) => body.iter().any(is_nested_block),
|
||||
Stmt::With(node) => node.body.iter().any(is_nested_block),
|
||||
_ => false,
|
||||
}
|
||||
}
|
||||
|
||||
@@ -69,54 +69,44 @@ fn num_statements(stmts: &[Stmt]) -> usize {
|
||||
let mut count = 0;
|
||||
for stmt in stmts {
|
||||
match stmt {
|
||||
Stmt::If(ast::StmtIf {
|
||||
body,
|
||||
elif_else_clauses,
|
||||
..
|
||||
}) => {
|
||||
Stmt::If(node) => {
|
||||
count += 1;
|
||||
count += num_statements(body);
|
||||
for clause in elif_else_clauses {
|
||||
count += num_statements(&node.body);
|
||||
for clause in &node.elif_else_clauses {
|
||||
count += 1;
|
||||
count += num_statements(&clause.body);
|
||||
}
|
||||
}
|
||||
Stmt::For(ast::StmtFor { body, orelse, .. }) => {
|
||||
count += num_statements(body);
|
||||
count += num_statements(orelse);
|
||||
Stmt::For(node) => {
|
||||
count += num_statements(&node.body);
|
||||
count += num_statements(&node.orelse);
|
||||
}
|
||||
Stmt::While(ast::StmtWhile { body, orelse, .. }) => {
|
||||
Stmt::While(node) => {
|
||||
count += 1;
|
||||
count += num_statements(body);
|
||||
count += num_statements(orelse);
|
||||
count += num_statements(&node.body);
|
||||
count += num_statements(&node.orelse);
|
||||
}
|
||||
Stmt::Match(ast::StmtMatch { cases, .. }) => {
|
||||
Stmt::Match(node) => {
|
||||
count += 1;
|
||||
for case in cases {
|
||||
for case in &node.cases {
|
||||
count += 1;
|
||||
count += num_statements(&case.body);
|
||||
}
|
||||
}
|
||||
Stmt::Try(ast::StmtTry {
|
||||
body,
|
||||
handlers,
|
||||
orelse,
|
||||
finalbody,
|
||||
..
|
||||
}) => {
|
||||
Stmt::Try(node) => {
|
||||
count += 1;
|
||||
count += num_statements(body);
|
||||
if !orelse.is_empty() {
|
||||
count += 1 + num_statements(orelse);
|
||||
count += num_statements(&node.body);
|
||||
if !node.orelse.is_empty() {
|
||||
count += 1 + num_statements(&node.orelse);
|
||||
}
|
||||
if !finalbody.is_empty() {
|
||||
if !node.finalbody.is_empty() {
|
||||
// Unclear why, but follow Pylint's convention.
|
||||
count += 2 + num_statements(finalbody);
|
||||
count += 2 + num_statements(&node.finalbody);
|
||||
}
|
||||
if handlers.len() > 1 {
|
||||
if node.handlers.len() > 1 {
|
||||
count += 1;
|
||||
}
|
||||
for handler in handlers {
|
||||
for handler in &node.handlers {
|
||||
count += 1;
|
||||
let ExceptHandler::ExceptHandler(ast::ExceptHandlerExceptHandler {
|
||||
body, ..
|
||||
@@ -124,10 +114,13 @@ fn num_statements(stmts: &[Stmt]) -> usize {
|
||||
count += num_statements(body);
|
||||
}
|
||||
}
|
||||
Stmt::FunctionDef(ast::StmtFunctionDef { body, .. })
|
||||
| Stmt::With(ast::StmtWith { body, .. }) => {
|
||||
Stmt::FunctionDef(node) => {
|
||||
count += 1;
|
||||
count += num_statements(body);
|
||||
count += num_statements(&node.body);
|
||||
}
|
||||
Stmt::With(node) => {
|
||||
count += 1;
|
||||
count += num_statements(&node.body);
|
||||
}
|
||||
Stmt::Return(_) => {}
|
||||
_ => {
|
||||
|
||||
@@ -87,39 +87,28 @@ pub(crate) fn useless_else_on_loop(checker: &Checker, stmt: &Stmt, body: &[Stmt]
|
||||
/// Returns `true` if the given body contains a `break` statement.
|
||||
fn loop_exits_early(body: &[Stmt]) -> bool {
|
||||
body.iter().any(|stmt| match stmt {
|
||||
Stmt::If(ast::StmtIf {
|
||||
body,
|
||||
elif_else_clauses,
|
||||
..
|
||||
}) => {
|
||||
loop_exits_early(body)
|
||||
|| elif_else_clauses
|
||||
Stmt::If(node) => {
|
||||
loop_exits_early(&node.body)
|
||||
|| node.elif_else_clauses
|
||||
.iter()
|
||||
.any(|clause| loop_exits_early(&clause.body))
|
||||
}
|
||||
Stmt::With(ast::StmtWith { body, .. }) => loop_exits_early(body),
|
||||
Stmt::Match(ast::StmtMatch { cases, .. }) => cases
|
||||
Stmt::With(node) => loop_exits_early(&node.body),
|
||||
Stmt::Match(node) => node.cases
|
||||
.iter()
|
||||
.any(|MatchCase { body, .. }| loop_exits_early(body)),
|
||||
Stmt::Try(ast::StmtTry {
|
||||
body,
|
||||
handlers,
|
||||
orelse,
|
||||
finalbody,
|
||||
..
|
||||
}) => {
|
||||
loop_exits_early(body)
|
||||
|| loop_exits_early(orelse)
|
||||
|| loop_exits_early(finalbody)
|
||||
|| handlers.iter().any(|handler| match handler {
|
||||
Stmt::Try(node) => {
|
||||
loop_exits_early(&node.body)
|
||||
|| loop_exits_early(&node.orelse)
|
||||
|| loop_exits_early(&node.finalbody)
|
||||
|| node.handlers.iter().any(|handler| match handler {
|
||||
ExceptHandler::ExceptHandler(ast::ExceptHandlerExceptHandler {
|
||||
body, ..
|
||||
}) => loop_exits_early(body),
|
||||
})
|
||||
}
|
||||
Stmt::For(ast::StmtFor { orelse, .. }) | Stmt::While(ast::StmtWhile { orelse, .. }) => {
|
||||
loop_exits_early(orelse)
|
||||
}
|
||||
Stmt::For(node) => loop_exits_early(&node.orelse),
|
||||
Stmt::While(node) => loop_exits_early(&node.orelse),
|
||||
Stmt::Break(_) => true,
|
||||
_ => false,
|
||||
})
|
||||
|
||||
@@ -55,11 +55,12 @@ where
|
||||
/// UP023
|
||||
pub(crate) fn deprecated_c_element_tree(checker: &Checker, stmt: &Stmt) {
|
||||
match stmt {
|
||||
Stmt::Import(ast::StmtImport {
|
||||
names,
|
||||
range: _,
|
||||
node_index: _,
|
||||
}) => {
|
||||
Stmt::Import(import_stmt) => {
|
||||
let ast::StmtImport {
|
||||
names,
|
||||
range: _,
|
||||
node_index: _,
|
||||
} = &**import_stmt;
|
||||
// Ex) `import xml.etree.cElementTree as ET`
|
||||
for name in names {
|
||||
if &name.name == "xml.etree.cElementTree" && name.asname.is_some() {
|
||||
@@ -67,13 +68,15 @@ pub(crate) fn deprecated_c_element_tree(checker: &Checker, stmt: &Stmt) {
|
||||
}
|
||||
}
|
||||
}
|
||||
Stmt::ImportFrom(ast::StmtImportFrom {
|
||||
module,
|
||||
names,
|
||||
level,
|
||||
range: _,
|
||||
node_index: _,
|
||||
}) => {
|
||||
Stmt::ImportFrom(import_from_stmt) => {
|
||||
let ast::StmtImportFrom {
|
||||
module,
|
||||
names,
|
||||
level,
|
||||
range: _,
|
||||
node_index: _,
|
||||
} = &**import_from_stmt;
|
||||
|
||||
if *level > 0 {
|
||||
// Ex) `import .xml.etree.cElementTree as ET`
|
||||
} else if let Some(module) = module {
|
||||
|
||||
@@ -277,11 +277,12 @@ pub(crate) fn deprecated_mock_attribute(checker: &Checker, attribute: &ast::Expr
|
||||
/// UP026
|
||||
pub(crate) fn deprecated_mock_import(checker: &Checker, stmt: &Stmt) {
|
||||
match stmt {
|
||||
Stmt::Import(ast::StmtImport {
|
||||
names,
|
||||
range: _,
|
||||
node_index: _,
|
||||
}) => {
|
||||
Stmt::Import(node) => {
|
||||
let ast::StmtImport {
|
||||
names,
|
||||
range: _,
|
||||
node_index: _,
|
||||
} = &**node;
|
||||
// Find all `mock` imports.
|
||||
if names
|
||||
.iter()
|
||||
@@ -326,12 +327,16 @@ pub(crate) fn deprecated_mock_import(checker: &Checker, stmt: &Stmt) {
|
||||
}
|
||||
}
|
||||
}
|
||||
Stmt::ImportFrom(ast::StmtImportFrom {
|
||||
module: Some(module),
|
||||
level,
|
||||
names,
|
||||
..
|
||||
}) => {
|
||||
Stmt::ImportFrom(node) => {
|
||||
let ast::StmtImportFrom {
|
||||
module: Some(module),
|
||||
level,
|
||||
names,
|
||||
range: _,
|
||||
node_index: _,
|
||||
} = &**node else {
|
||||
return;
|
||||
};
|
||||
if *level > 0 {
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -97,15 +97,13 @@ pub(crate) fn super_call_with_parameters(checker: &Checker, call: &ast::ExprCall
|
||||
};
|
||||
|
||||
// Find the enclosing function definition (if any).
|
||||
let Some(
|
||||
func_stmt @ Stmt::FunctionDef(ast::StmtFunctionDef {
|
||||
parameters: parent_parameters,
|
||||
..
|
||||
}),
|
||||
) = parents.find(|stmt| stmt.is_function_def_stmt())
|
||||
else {
|
||||
let Some(func_stmt) = parents.find(|stmt| stmt.is_function_def_stmt()) else {
|
||||
return;
|
||||
};
|
||||
let Stmt::FunctionDef(func_def) = func_stmt else {
|
||||
return;
|
||||
};
|
||||
let parent_parameters = &func_def.parameters;
|
||||
|
||||
if is_builtins_super(checker.semantic(), call)
|
||||
&& !has_local_dunder_class_var_ref(checker.semantic(), func_stmt)
|
||||
@@ -119,14 +117,14 @@ pub(crate) fn super_call_with_parameters(checker: &Checker, call: &ast::ExprCall
|
||||
};
|
||||
|
||||
// Find the enclosing class definition (if any).
|
||||
let Some(Stmt::ClassDef(ast::StmtClassDef {
|
||||
name: parent_name,
|
||||
decorator_list,
|
||||
..
|
||||
})) = parents.find(|stmt| stmt.is_class_def_stmt())
|
||||
else {
|
||||
let Some(class_stmt) = parents.find(|stmt| stmt.is_class_def_stmt()) else {
|
||||
return;
|
||||
};
|
||||
let Stmt::ClassDef(class_def) = class_stmt else {
|
||||
return;
|
||||
};
|
||||
let parent_name = &class_def.name;
|
||||
let decorator_list = &class_def.decorator_list;
|
||||
|
||||
let (
|
||||
Expr::Name(ast::ExprName {
|
||||
@@ -252,13 +250,11 @@ impl ClassCellReferenceFinder {
|
||||
|
||||
impl<'a> Visitor<'a> for ClassCellReferenceFinder {
|
||||
fn visit_stmt(&mut self, stmt: &'a Stmt) {
|
||||
match stmt {
|
||||
Stmt::ClassDef(_) => {}
|
||||
_ => {
|
||||
if !self.has_class_cell {
|
||||
walk_stmt(self, stmt);
|
||||
}
|
||||
}
|
||||
if stmt.is_class_def_stmt() {
|
||||
return;
|
||||
}
|
||||
if !self.has_class_cell {
|
||||
walk_stmt(self, stmt);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -140,7 +140,8 @@ pub(crate) fn unnecessary_future_import(checker: &Checker, scope: &Scope) {
|
||||
};
|
||||
|
||||
let stmt = checker.semantic().statement(node_id);
|
||||
if let Stmt::ImportFrom(ast::StmtImportFrom { names, .. }) = stmt {
|
||||
if let Stmt::ImportFrom(node) = stmt {
|
||||
let names = &node.names;
|
||||
let Some(alias) = names
|
||||
.iter()
|
||||
.find(|alias| alias.name.as_str() == binding.name(checker.source()))
|
||||
|
||||
@@ -902,76 +902,56 @@ help: Convert to f-string
|
||||
132 | # Non-errors
|
||||
|
||||
UP032 [*] Use f-string instead of `format` call
|
||||
--> UP032_0.py:135:1
|
||||
--> UP032_0.py:160:1
|
||||
|
|
||||
133 | ###
|
||||
134 |
|
||||
135 | "\N{snowman} {}".format(a)
|
||||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^
|
||||
136 |
|
||||
137 | "{".format(a)
|
||||
|
|
||||
help: Convert to f-string
|
||||
132 | # Non-errors
|
||||
133 | ###
|
||||
134 |
|
||||
- "\N{snowman} {}".format(a)
|
||||
135 + f"\N{snowman} {a}"
|
||||
136 |
|
||||
137 | "{".format(a)
|
||||
138 |
|
||||
|
||||
UP032 [*] Use f-string instead of `format` call
|
||||
--> UP032_0.py:159:1
|
||||
|
|
||||
157 | r'"\N{snowman} {}".format(a)'
|
||||
158 |
|
||||
159 | / "123456789 {}".format(
|
||||
160 | | 11111111111111111111111111111111111111111111111111111111111111111111111111,
|
||||
161 | | )
|
||||
158 | r'"\N{snowman} {}".format(a)'
|
||||
159 |
|
||||
160 | / "123456789 {}".format(
|
||||
161 | | 11111111111111111111111111111111111111111111111111111111111111111111111111,
|
||||
162 | | )
|
||||
| |_^
|
||||
162 |
|
||||
163 | """
|
||||
163 |
|
||||
164 | """
|
||||
|
|
||||
help: Convert to f-string
|
||||
156 |
|
||||
157 | r'"\N{snowman} {}".format(a)'
|
||||
158 |
|
||||
157 |
|
||||
158 | r'"\N{snowman} {}".format(a)'
|
||||
159 |
|
||||
- "123456789 {}".format(
|
||||
- 11111111111111111111111111111111111111111111111111111111111111111111111111,
|
||||
- )
|
||||
159 + f"123456789 {11111111111111111111111111111111111111111111111111111111111111111111111111}"
|
||||
160 |
|
||||
161 | """
|
||||
162 | {}
|
||||
160 + f"123456789 {11111111111111111111111111111111111111111111111111111111111111111111111111}"
|
||||
161 |
|
||||
162 | """
|
||||
163 | {}
|
||||
|
||||
UP032 [*] Use f-string instead of `format` call
|
||||
--> UP032_0.py:163:1
|
||||
--> UP032_0.py:164:1
|
||||
|
|
||||
161 | )
|
||||
162 |
|
||||
163 | / """
|
||||
164 | | {}
|
||||
162 | )
|
||||
163 |
|
||||
164 | / """
|
||||
165 | | {}
|
||||
166 | | {}
|
||||
167 | | """.format(
|
||||
168 | | 1,
|
||||
169 | | 2,
|
||||
170 | | 111111111111111111111111111111111111111111111111111111111111111111111111111111111111111,
|
||||
171 | | )
|
||||
167 | | {}
|
||||
168 | | """.format(
|
||||
169 | | 1,
|
||||
170 | | 2,
|
||||
171 | | 111111111111111111111111111111111111111111111111111111111111111111111111111111111111111,
|
||||
172 | | )
|
||||
| |_^
|
||||
172 |
|
||||
173 | aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa = """{}
|
||||
173 |
|
||||
174 | aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa = """{}
|
||||
|
|
||||
help: Convert to f-string
|
||||
160 | 11111111111111111111111111111111111111111111111111111111111111111111111111,
|
||||
161 | )
|
||||
162 |
|
||||
163 + f"""
|
||||
164 + {1}
|
||||
165 + {2}
|
||||
166 + {111111111111111111111111111111111111111111111111111111111111111111111111111111111111111}
|
||||
167 | """
|
||||
161 | 11111111111111111111111111111111111111111111111111111111111111111111111111,
|
||||
162 | )
|
||||
163 |
|
||||
164 + f"""
|
||||
165 + {1}
|
||||
166 + {2}
|
||||
167 + {111111111111111111111111111111111111111111111111111111111111111111111111111111111111111}
|
||||
168 | """
|
||||
- {}
|
||||
- {}
|
||||
- {}
|
||||
@@ -980,408 +960,392 @@ help: Convert to f-string
|
||||
- 2,
|
||||
- 111111111111111111111111111111111111111111111111111111111111111111111111111111111111111,
|
||||
- )
|
||||
168 |
|
||||
169 | aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa = """{}
|
||||
170 | """.format(
|
||||
169 |
|
||||
170 | aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa = """{}
|
||||
171 | """.format(
|
||||
|
||||
UP032 [*] Use f-string instead of `format` call
|
||||
--> UP032_0.py:173:84
|
||||
--> UP032_0.py:174:84
|
||||
|
|
||||
171 | )
|
||||
172 |
|
||||
173 | aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa = """{}
|
||||
172 | )
|
||||
173 |
|
||||
174 | aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa = """{}
|
||||
| ____________________________________________________________________________________^
|
||||
174 | | """.format(
|
||||
175 | | 111111
|
||||
176 | | )
|
||||
175 | | """.format(
|
||||
176 | | 111111
|
||||
177 | | )
|
||||
| |_^
|
||||
177 |
|
||||
178 | "{}".format(
|
||||
178 |
|
||||
179 | "{}".format(
|
||||
|
|
||||
help: Convert to f-string
|
||||
170 | 111111111111111111111111111111111111111111111111111111111111111111111111111111111111111,
|
||||
171 | )
|
||||
172 |
|
||||
171 | 111111111111111111111111111111111111111111111111111111111111111111111111111111111111111,
|
||||
172 | )
|
||||
173 |
|
||||
- aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa = """{}
|
||||
- """.format(
|
||||
- 111111
|
||||
- )
|
||||
173 + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa = f"""{111111}
|
||||
174 + """
|
||||
175 |
|
||||
176 | "{}".format(
|
||||
177 | [
|
||||
174 + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa = f"""{111111}
|
||||
175 + """
|
||||
176 |
|
||||
177 | "{}".format(
|
||||
178 | [
|
||||
|
||||
UP032 Use f-string instead of `format` call
|
||||
--> UP032_0.py:201:1
|
||||
--> UP032_0.py:202:1
|
||||
|
|
||||
199 | "{}".format(**c)
|
||||
200 |
|
||||
201 | / "{}".format(
|
||||
202 | | 1 # comment
|
||||
203 | | )
|
||||
200 | "{}".format(**c)
|
||||
201 |
|
||||
202 | / "{}".format(
|
||||
203 | | 1 # comment
|
||||
204 | | )
|
||||
| |_^
|
||||
|
|
||||
help: Convert to f-string
|
||||
|
||||
UP032 [*] Use f-string instead of `format` call
|
||||
--> UP032_0.py:208:1
|
||||
--> UP032_0.py:209:1
|
||||
|
|
||||
206 | # The fixed string will exceed the line length, but it's still smaller than the
|
||||
207 | # existing line length, so it's fine.
|
||||
208 | "<Customer: {}, {}, {}, {}, {}>".format(self.internal_ids, self.external_ids, self.properties, self.tags, self.others)
|
||||
207 | # The fixed string will exceed the line length, but it's still smaller than the
|
||||
208 | # existing line length, so it's fine.
|
||||
209 | "<Customer: {}, {}, {}, {}, {}>".format(self.internal_ids, self.external_ids, self.properties, self.tags, self.others)
|
||||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
||||
209 |
|
||||
210 | # When fixing, trim the trailing empty string.
|
||||
210 |
|
||||
211 | # When fixing, trim the trailing empty string.
|
||||
|
|
||||
help: Convert to f-string
|
||||
205 |
|
||||
206 | # The fixed string will exceed the line length, but it's still smaller than the
|
||||
207 | # existing line length, so it's fine.
|
||||
206 |
|
||||
207 | # The fixed string will exceed the line length, but it's still smaller than the
|
||||
208 | # existing line length, so it's fine.
|
||||
- "<Customer: {}, {}, {}, {}, {}>".format(self.internal_ids, self.external_ids, self.properties, self.tags, self.others)
|
||||
208 + f"<Customer: {self.internal_ids}, {self.external_ids}, {self.properties}, {self.tags}, {self.others}>"
|
||||
209 |
|
||||
210 | # When fixing, trim the trailing empty string.
|
||||
211 | raise ValueError("Conflicting configuration dicts: {!r} {!r}"
|
||||
209 + f"<Customer: {self.internal_ids}, {self.external_ids}, {self.properties}, {self.tags}, {self.others}>"
|
||||
210 |
|
||||
211 | # When fixing, trim the trailing empty string.
|
||||
212 | raise ValueError("Conflicting configuration dicts: {!r} {!r}"
|
||||
|
||||
UP032 [*] Use f-string instead of `format` call
|
||||
--> UP032_0.py:211:18
|
||||
--> UP032_0.py:212:18
|
||||
|
|
||||
210 | # When fixing, trim the trailing empty string.
|
||||
211 | raise ValueError("Conflicting configuration dicts: {!r} {!r}"
|
||||
211 | # When fixing, trim the trailing empty string.
|
||||
212 | raise ValueError("Conflicting configuration dicts: {!r} {!r}"
|
||||
| __________________^
|
||||
212 | | "".format(new_dict, d))
|
||||
213 | | "".format(new_dict, d))
|
||||
| |_______________________________________^
|
||||
213 |
|
||||
214 | # When fixing, trim the trailing empty string.
|
||||
214 |
|
||||
215 | # When fixing, trim the trailing empty string.
|
||||
|
|
||||
help: Convert to f-string
|
||||
208 | "<Customer: {}, {}, {}, {}, {}>".format(self.internal_ids, self.external_ids, self.properties, self.tags, self.others)
|
||||
209 |
|
||||
210 | # When fixing, trim the trailing empty string.
|
||||
209 | "<Customer: {}, {}, {}, {}, {}>".format(self.internal_ids, self.external_ids, self.properties, self.tags, self.others)
|
||||
210 |
|
||||
211 | # When fixing, trim the trailing empty string.
|
||||
- raise ValueError("Conflicting configuration dicts: {!r} {!r}"
|
||||
- "".format(new_dict, d))
|
||||
211 + raise ValueError(f"Conflicting configuration dicts: {new_dict!r} {d!r}")
|
||||
212 |
|
||||
213 | # When fixing, trim the trailing empty string.
|
||||
214 | raise ValueError("Conflicting configuration dicts: {!r} {!r}"
|
||||
|
||||
UP032 [*] Use f-string instead of `format` call
|
||||
--> UP032_0.py:215:18
|
||||
|
|
||||
214 | # When fixing, trim the trailing empty string.
|
||||
215 | raise ValueError("Conflicting configuration dicts: {!r} {!r}"
|
||||
| __________________^
|
||||
216 | | .format(new_dict, d))
|
||||
| |_____________________________________^
|
||||
217 |
|
||||
218 | raise ValueError(
|
||||
|
|
||||
help: Convert to f-string
|
||||
212 | "".format(new_dict, d))
|
||||
212 + raise ValueError(f"Conflicting configuration dicts: {new_dict!r} {d!r}")
|
||||
213 |
|
||||
214 | # When fixing, trim the trailing empty string.
|
||||
215 | raise ValueError("Conflicting configuration dicts: {!r} {!r}"
|
||||
|
||||
UP032 [*] Use f-string instead of `format` call
|
||||
--> UP032_0.py:216:18
|
||||
|
|
||||
215 | # When fixing, trim the trailing empty string.
|
||||
216 | raise ValueError("Conflicting configuration dicts: {!r} {!r}"
|
||||
| __________________^
|
||||
217 | | .format(new_dict, d))
|
||||
| |_____________________________________^
|
||||
218 |
|
||||
219 | raise ValueError(
|
||||
|
|
||||
help: Convert to f-string
|
||||
213 | "".format(new_dict, d))
|
||||
214 |
|
||||
215 | # When fixing, trim the trailing empty string.
|
||||
- raise ValueError("Conflicting configuration dicts: {!r} {!r}"
|
||||
- .format(new_dict, d))
|
||||
215 + raise ValueError(f"Conflicting configuration dicts: {new_dict!r} {d!r}"
|
||||
216 + )
|
||||
217 |
|
||||
218 | raise ValueError(
|
||||
219 | "Conflicting configuration dicts: {!r} {!r}"
|
||||
216 + raise ValueError(f"Conflicting configuration dicts: {new_dict!r} {d!r}"
|
||||
217 + )
|
||||
218 |
|
||||
219 | raise ValueError(
|
||||
220 | "Conflicting configuration dicts: {!r} {!r}"
|
||||
|
||||
UP032 [*] Use f-string instead of `format` call
|
||||
--> UP032_0.py:219:5
|
||||
--> UP032_0.py:220:5
|
||||
|
|
||||
218 | raise ValueError(
|
||||
219 | / "Conflicting configuration dicts: {!r} {!r}"
|
||||
220 | | "".format(new_dict, d)
|
||||
219 | raise ValueError(
|
||||
220 | / "Conflicting configuration dicts: {!r} {!r}"
|
||||
221 | | "".format(new_dict, d)
|
||||
| |__________________________^
|
||||
221 | )
|
||||
222 | )
|
||||
|
|
||||
help: Convert to f-string
|
||||
216 | .format(new_dict, d))
|
||||
217 |
|
||||
218 | raise ValueError(
|
||||
217 | .format(new_dict, d))
|
||||
218 |
|
||||
219 | raise ValueError(
|
||||
- "Conflicting configuration dicts: {!r} {!r}"
|
||||
- "".format(new_dict, d)
|
||||
219 + f"Conflicting configuration dicts: {new_dict!r} {d!r}"
|
||||
220 | )
|
||||
221 |
|
||||
222 | raise ValueError(
|
||||
|
||||
UP032 [*] Use f-string instead of `format` call
|
||||
--> UP032_0.py:224:5
|
||||
|
|
||||
223 | raise ValueError(
|
||||
224 | / "Conflicting configuration dicts: {!r} {!r}"
|
||||
225 | | "".format(new_dict, d)
|
||||
| |__________________________^
|
||||
226 |
|
||||
227 | )
|
||||
|
|
||||
help: Convert to f-string
|
||||
220 + f"Conflicting configuration dicts: {new_dict!r} {d!r}"
|
||||
221 | )
|
||||
222 |
|
||||
223 | raise ValueError(
|
||||
- "Conflicting configuration dicts: {!r} {!r}"
|
||||
- "".format(new_dict, d)
|
||||
224 + f"Conflicting configuration dicts: {new_dict!r} {d!r}"
|
||||
225 |
|
||||
226 | )
|
||||
227 |
|
||||
|
||||
UP032 [*] Use f-string instead of `format` call
|
||||
--> UP032_0.py:230:1
|
||||
--> UP032_0.py:225:5
|
||||
|
|
||||
229 | # The first string will be converted to an f-string and the curly braces in the second should be converted to be unescaped
|
||||
230 | / (
|
||||
231 | | "{}"
|
||||
232 | | "{{}}"
|
||||
233 | | ).format(a)
|
||||
| |___________^
|
||||
234 |
|
||||
235 | ("{}" "{{}}").format(a)
|
||||
224 | raise ValueError(
|
||||
225 | / "Conflicting configuration dicts: {!r} {!r}"
|
||||
226 | | "".format(new_dict, d)
|
||||
| |__________________________^
|
||||
227 |
|
||||
228 | )
|
||||
|
|
||||
help: Convert to f-string
|
||||
222 | )
|
||||
223 |
|
||||
224 | raise ValueError(
|
||||
- "Conflicting configuration dicts: {!r} {!r}"
|
||||
- "".format(new_dict, d)
|
||||
225 + f"Conflicting configuration dicts: {new_dict!r} {d!r}"
|
||||
226 |
|
||||
227 | )
|
||||
228 |
|
||||
229 | # The first string will be converted to an f-string and the curly braces in the second should be converted to be unescaped
|
||||
230 | (
|
||||
231 + f"{a}"
|
||||
232 | "{}"
|
||||
- "{{}}"
|
||||
- ).format(a)
|
||||
233 + )
|
||||
234 |
|
||||
235 | ("{}" "{{}}").format(a)
|
||||
236 |
|
||||
|
||||
UP032 [*] Use f-string instead of `format` call
|
||||
--> UP032_0.py:235:1
|
||||
--> UP032_0.py:231:1
|
||||
|
|
||||
233 | ).format(a)
|
||||
234 |
|
||||
235 | ("{}" "{{}}").format(a)
|
||||
230 | # The first string will be converted to an f-string and the curly braces in the second should be converted to be unescaped
|
||||
231 | / (
|
||||
232 | | "{}"
|
||||
233 | | "{{}}"
|
||||
234 | | ).format(a)
|
||||
| |___________^
|
||||
235 |
|
||||
236 | ("{}" "{{}}").format(a)
|
||||
|
|
||||
help: Convert to f-string
|
||||
229 |
|
||||
230 | # The first string will be converted to an f-string and the curly braces in the second should be converted to be unescaped
|
||||
231 | (
|
||||
232 + f"{a}"
|
||||
233 | "{}"
|
||||
- "{{}}"
|
||||
- ).format(a)
|
||||
234 + )
|
||||
235 |
|
||||
236 | ("{}" "{{}}").format(a)
|
||||
237 |
|
||||
|
||||
UP032 [*] Use f-string instead of `format` call
|
||||
--> UP032_0.py:236:1
|
||||
|
|
||||
234 | ).format(a)
|
||||
235 |
|
||||
236 | ("{}" "{{}}").format(a)
|
||||
| ^^^^^^^^^^^^^^^^^^^^^^^
|
||||
|
|
||||
help: Convert to f-string
|
||||
232 | "{{}}"
|
||||
233 | ).format(a)
|
||||
234 |
|
||||
233 | "{{}}"
|
||||
234 | ).format(a)
|
||||
235 |
|
||||
- ("{}" "{{}}").format(a)
|
||||
235 + (f"{a}" "{}")
|
||||
236 |
|
||||
236 + (f"{a}" "{}")
|
||||
237 |
|
||||
238 | # Both strings will be converted to an f-string and the curly braces in the second should left escaped
|
||||
238 |
|
||||
239 | # Both strings will be converted to an f-string and the curly braces in the second should left escaped
|
||||
|
||||
UP032 [*] Use f-string instead of `format` call
|
||||
--> UP032_0.py:239:1
|
||||
--> UP032_0.py:240:1
|
||||
|
|
||||
238 | # Both strings will be converted to an f-string and the curly braces in the second should left escaped
|
||||
239 | / (
|
||||
240 | | "{}"
|
||||
241 | | "{{{}}}"
|
||||
242 | | ).format(a, b)
|
||||
239 | # Both strings will be converted to an f-string and the curly braces in the second should left escaped
|
||||
240 | / (
|
||||
241 | | "{}"
|
||||
242 | | "{{{}}}"
|
||||
243 | | ).format(a, b)
|
||||
| |______________^
|
||||
243 |
|
||||
244 | ("{}" "{{{}}}").format(a, b)
|
||||
244 |
|
||||
245 | ("{}" "{{{}}}").format(a, b)
|
||||
|
|
||||
help: Convert to f-string
|
||||
237 |
|
||||
238 | # Both strings will be converted to an f-string and the curly braces in the second should left escaped
|
||||
239 | (
|
||||
238 |
|
||||
239 | # Both strings will be converted to an f-string and the curly braces in the second should left escaped
|
||||
240 | (
|
||||
- "{}"
|
||||
- "{{{}}}"
|
||||
- ).format(a, b)
|
||||
240 + f"{a}"
|
||||
241 + f"{{{b}}}"
|
||||
242 + )
|
||||
243 |
|
||||
244 | ("{}" "{{{}}}").format(a, b)
|
||||
245 |
|
||||
241 + f"{a}"
|
||||
242 + f"{{{b}}}"
|
||||
243 + )
|
||||
244 |
|
||||
245 | ("{}" "{{{}}}").format(a, b)
|
||||
246 |
|
||||
|
||||
UP032 [*] Use f-string instead of `format` call
|
||||
--> UP032_0.py:244:1
|
||||
--> UP032_0.py:245:1
|
||||
|
|
||||
242 | ).format(a, b)
|
||||
243 |
|
||||
244 | ("{}" "{{{}}}").format(a, b)
|
||||
243 | ).format(a, b)
|
||||
244 |
|
||||
245 | ("{}" "{{{}}}").format(a, b)
|
||||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
||||
245 |
|
||||
246 | # The dictionary should be parenthesized.
|
||||
246 |
|
||||
247 | # The dictionary should be parenthesized.
|
||||
|
|
||||
help: Convert to f-string
|
||||
241 | "{{{}}}"
|
||||
242 | ).format(a, b)
|
||||
243 |
|
||||
242 | "{{{}}}"
|
||||
243 | ).format(a, b)
|
||||
244 |
|
||||
- ("{}" "{{{}}}").format(a, b)
|
||||
244 + (f"{a}" f"{{{b}}}")
|
||||
245 |
|
||||
246 | # The dictionary should be parenthesized.
|
||||
247 | "{}".format({0: 1}[0])
|
||||
245 + (f"{a}" f"{{{b}}}")
|
||||
246 |
|
||||
247 | # The dictionary should be parenthesized.
|
||||
248 | "{}".format({0: 1}[0])
|
||||
|
||||
UP032 [*] Use f-string instead of `format` call
|
||||
--> UP032_0.py:247:1
|
||||
--> UP032_0.py:248:1
|
||||
|
|
||||
246 | # The dictionary should be parenthesized.
|
||||
247 | "{}".format({0: 1}[0])
|
||||
247 | # The dictionary should be parenthesized.
|
||||
248 | "{}".format({0: 1}[0])
|
||||
| ^^^^^^^^^^^^^^^^^^^^^^
|
||||
248 |
|
||||
249 | # The dictionary should be parenthesized.
|
||||
249 |
|
||||
250 | # The dictionary should be parenthesized.
|
||||
|
|
||||
help: Convert to f-string
|
||||
244 | ("{}" "{{{}}}").format(a, b)
|
||||
245 |
|
||||
246 | # The dictionary should be parenthesized.
|
||||
245 | ("{}" "{{{}}}").format(a, b)
|
||||
246 |
|
||||
247 | # The dictionary should be parenthesized.
|
||||
- "{}".format({0: 1}[0])
|
||||
247 + f"{({0: 1}[0])}"
|
||||
248 |
|
||||
249 | # The dictionary should be parenthesized.
|
||||
250 | "{}".format({0: 1}.bar)
|
||||
248 + f"{({0: 1}[0])}"
|
||||
249 |
|
||||
250 | # The dictionary should be parenthesized.
|
||||
251 | "{}".format({0: 1}.bar)
|
||||
|
||||
UP032 [*] Use f-string instead of `format` call
|
||||
--> UP032_0.py:250:1
|
||||
--> UP032_0.py:251:1
|
||||
|
|
||||
249 | # The dictionary should be parenthesized.
|
||||
250 | "{}".format({0: 1}.bar)
|
||||
250 | # The dictionary should be parenthesized.
|
||||
251 | "{}".format({0: 1}.bar)
|
||||
| ^^^^^^^^^^^^^^^^^^^^^^^
|
||||
251 |
|
||||
252 | # The dictionary should be parenthesized.
|
||||
252 |
|
||||
253 | # The dictionary should be parenthesized.
|
||||
|
|
||||
help: Convert to f-string
|
||||
247 | "{}".format({0: 1}[0])
|
||||
248 |
|
||||
249 | # The dictionary should be parenthesized.
|
||||
248 | "{}".format({0: 1}[0])
|
||||
249 |
|
||||
250 | # The dictionary should be parenthesized.
|
||||
- "{}".format({0: 1}.bar)
|
||||
250 + f"{({0: 1}.bar)}"
|
||||
251 |
|
||||
252 | # The dictionary should be parenthesized.
|
||||
253 | "{}".format({0: 1}())
|
||||
251 + f"{({0: 1}.bar)}"
|
||||
252 |
|
||||
253 | # The dictionary should be parenthesized.
|
||||
254 | "{}".format({0: 1}())
|
||||
|
||||
UP032 [*] Use f-string instead of `format` call
|
||||
--> UP032_0.py:253:1
|
||||
--> UP032_0.py:254:1
|
||||
|
|
||||
252 | # The dictionary should be parenthesized.
|
||||
253 | "{}".format({0: 1}())
|
||||
253 | # The dictionary should be parenthesized.
|
||||
254 | "{}".format({0: 1}())
|
||||
| ^^^^^^^^^^^^^^^^^^^^^
|
||||
254 |
|
||||
255 | # The string shouldn't be converted, since it would require repeating the function call.
|
||||
255 |
|
||||
256 | # The string shouldn't be converted, since it would require repeating the function call.
|
||||
|
|
||||
help: Convert to f-string
|
||||
250 | "{}".format({0: 1}.bar)
|
||||
251 |
|
||||
252 | # The dictionary should be parenthesized.
|
||||
251 | "{}".format({0: 1}.bar)
|
||||
252 |
|
||||
253 | # The dictionary should be parenthesized.
|
||||
- "{}".format({0: 1}())
|
||||
253 + f"{({0: 1}())}"
|
||||
254 |
|
||||
255 | # The string shouldn't be converted, since it would require repeating the function call.
|
||||
256 | "{x} {x}".format(x=foo())
|
||||
254 + f"{({0: 1}())}"
|
||||
255 |
|
||||
256 | # The string shouldn't be converted, since it would require repeating the function call.
|
||||
257 | "{x} {x}".format(x=foo())
|
||||
|
||||
UP032 [*] Use f-string instead of `format` call
|
||||
--> UP032_0.py:260:1
|
||||
--> UP032_0.py:261:1
|
||||
|
|
||||
259 | # The string _should_ be converted, since the function call is repeated in the arguments.
|
||||
260 | "{0} {1}".format(foo(), foo())
|
||||
260 | # The string _should_ be converted, since the function call is repeated in the arguments.
|
||||
261 | "{0} {1}".format(foo(), foo())
|
||||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
||||
261 |
|
||||
262 | # The call should be removed, but the string itself should remain.
|
||||
262 |
|
||||
263 | # The call should be removed, but the string itself should remain.
|
||||
|
|
||||
help: Convert to f-string
|
||||
257 | "{0} {0}".format(foo())
|
||||
258 |
|
||||
259 | # The string _should_ be converted, since the function call is repeated in the arguments.
|
||||
258 | "{0} {0}".format(foo())
|
||||
259 |
|
||||
260 | # The string _should_ be converted, since the function call is repeated in the arguments.
|
||||
- "{0} {1}".format(foo(), foo())
|
||||
260 + f"{foo()} {foo()}"
|
||||
261 |
|
||||
262 | # The call should be removed, but the string itself should remain.
|
||||
263 | ''.format(self.project)
|
||||
261 + f"{foo()} {foo()}"
|
||||
262 |
|
||||
263 | # The call should be removed, but the string itself should remain.
|
||||
264 | ''.format(self.project)
|
||||
|
||||
UP032 [*] Use f-string instead of `format` call
|
||||
--> UP032_0.py:263:1
|
||||
--> UP032_0.py:264:1
|
||||
|
|
||||
262 | # The call should be removed, but the string itself should remain.
|
||||
263 | ''.format(self.project)
|
||||
263 | # The call should be removed, but the string itself should remain.
|
||||
264 | ''.format(self.project)
|
||||
| ^^^^^^^^^^^^^^^^^^^^^^^
|
||||
264 |
|
||||
265 | # The call should be removed, but the string itself should remain.
|
||||
265 |
|
||||
266 | # The call should be removed, but the string itself should remain.
|
||||
|
|
||||
help: Convert to f-string
|
||||
260 | "{0} {1}".format(foo(), foo())
|
||||
261 |
|
||||
262 | # The call should be removed, but the string itself should remain.
|
||||
261 | "{0} {1}".format(foo(), foo())
|
||||
262 |
|
||||
263 | # The call should be removed, but the string itself should remain.
|
||||
- ''.format(self.project)
|
||||
263 + ''
|
||||
264 |
|
||||
265 | # The call should be removed, but the string itself should remain.
|
||||
266 | "".format(self.project)
|
||||
264 + ''
|
||||
265 |
|
||||
266 | # The call should be removed, but the string itself should remain.
|
||||
267 | "".format(self.project)
|
||||
|
||||
UP032 [*] Use f-string instead of `format` call
|
||||
--> UP032_0.py:266:1
|
||||
--> UP032_0.py:267:1
|
||||
|
|
||||
265 | # The call should be removed, but the string itself should remain.
|
||||
266 | "".format(self.project)
|
||||
266 | # The call should be removed, but the string itself should remain.
|
||||
267 | "".format(self.project)
|
||||
| ^^^^^^^^^^^^^^^^^^^^^^^
|
||||
267 |
|
||||
268 | # Not a valid type annotation but this test shouldn't result in a panic.
|
||||
268 |
|
||||
269 | # Not a valid type annotation but this test shouldn't result in a panic.
|
||||
|
|
||||
help: Convert to f-string
|
||||
263 | ''.format(self.project)
|
||||
264 |
|
||||
265 | # The call should be removed, but the string itself should remain.
|
||||
264 | ''.format(self.project)
|
||||
265 |
|
||||
266 | # The call should be removed, but the string itself should remain.
|
||||
- "".format(self.project)
|
||||
266 + ""
|
||||
267 |
|
||||
268 | # Not a valid type annotation but this test shouldn't result in a panic.
|
||||
269 | # Refer: https://github.com/astral-sh/ruff/issues/11736
|
||||
267 + ""
|
||||
268 |
|
||||
269 | # Not a valid type annotation but this test shouldn't result in a panic.
|
||||
270 | # Refer: https://github.com/astral-sh/ruff/issues/11736
|
||||
|
||||
UP032 [*] Use f-string instead of `format` call
|
||||
--> UP032_0.py:270:5
|
||||
--> UP032_0.py:271:5
|
||||
|
|
||||
268 | # Not a valid type annotation but this test shouldn't result in a panic.
|
||||
269 | # Refer: https://github.com/astral-sh/ruff/issues/11736
|
||||
270 | x: "'{} + {}'.format(x, y)"
|
||||
269 | # Not a valid type annotation but this test shouldn't result in a panic.
|
||||
270 | # Refer: https://github.com/astral-sh/ruff/issues/11736
|
||||
271 | x: "'{} + {}'.format(x, y)"
|
||||
| ^^^^^^^^^^^^^^^^^^^^^^
|
||||
271 |
|
||||
272 | # Regression https://github.com/astral-sh/ruff/issues/21000
|
||||
272 |
|
||||
273 | # Regression https://github.com/astral-sh/ruff/issues/21000
|
||||
|
|
||||
help: Convert to f-string
|
||||
267 |
|
||||
268 | # Not a valid type annotation but this test shouldn't result in a panic.
|
||||
269 | # Refer: https://github.com/astral-sh/ruff/issues/11736
|
||||
268 |
|
||||
269 | # Not a valid type annotation but this test shouldn't result in a panic.
|
||||
270 | # Refer: https://github.com/astral-sh/ruff/issues/11736
|
||||
- x: "'{} + {}'.format(x, y)"
|
||||
270 + x: "f'{x} + {y}'"
|
||||
271 |
|
||||
272 | # Regression https://github.com/astral-sh/ruff/issues/21000
|
||||
273 | # Fix should parenthesize walrus
|
||||
271 + x: "f'{x} + {y}'"
|
||||
272 |
|
||||
273 | # Regression https://github.com/astral-sh/ruff/issues/21000
|
||||
274 | # Fix should parenthesize walrus
|
||||
|
||||
UP032 [*] Use f-string instead of `format` call
|
||||
--> UP032_0.py:276:14
|
||||
--> UP032_0.py:277:14
|
||||
|
|
||||
274 | if __name__ == "__main__":
|
||||
275 | number = 0
|
||||
276 | string = "{}".format(number := number + 1)
|
||||
275 | if __name__ == "__main__":
|
||||
276 | number = 0
|
||||
277 | string = "{}".format(number := number + 1)
|
||||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
||||
277 | print(string)
|
||||
278 | print(string)
|
||||
|
|
||||
help: Convert to f-string
|
||||
273 | # Fix should parenthesize walrus
|
||||
274 | if __name__ == "__main__":
|
||||
275 | number = 0
|
||||
274 | # Fix should parenthesize walrus
|
||||
275 | if __name__ == "__main__":
|
||||
276 | number = 0
|
||||
- string = "{}".format(number := number + 1)
|
||||
276 + string = f"{(number := number + 1)}"
|
||||
277 | print(string)
|
||||
278 |
|
||||
279 | # Unicode escape
|
||||
|
||||
UP032 [*] Use f-string instead of `format` call
|
||||
--> UP032_0.py:280:1
|
||||
|
|
||||
279 | # Unicode escape
|
||||
280 | "\N{angle}AOB = {angle}°".format(angle=180)
|
||||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
||||
|
|
||||
help: Convert to f-string
|
||||
277 | print(string)
|
||||
278 |
|
||||
279 | # Unicode escape
|
||||
- "\N{angle}AOB = {angle}°".format(angle=180)
|
||||
280 + f"\N{angle}AOB = {180}°"
|
||||
277 + string = f"{(number := number + 1)}"
|
||||
278 | print(string)
|
||||
|
||||
@@ -203,8 +203,8 @@ fn generate_fix(
|
||||
// The assignment's RHS must also be the same as the `read` call in `expr`, otherwise this fix
|
||||
// would remove the rest of the expression.
|
||||
let replacement = match with_stmt.body.as_slice() {
|
||||
[Stmt::Assign(ast::StmtAssign { targets, value, .. })] if value.range() == expr.range() => {
|
||||
match targets.as_slice() {
|
||||
[Stmt::Assign(assign_node)] if assign_node.value.range() == expr.range() => {
|
||||
match assign_node.targets.as_slice() {
|
||||
[Expr::Name(name)] => {
|
||||
format!(
|
||||
"{name} = {binding}({filename_code}).{suggestion}",
|
||||
@@ -214,23 +214,18 @@ fn generate_fix(
|
||||
_ => return None,
|
||||
}
|
||||
}
|
||||
[
|
||||
Stmt::AnnAssign(ast::StmtAnnAssign {
|
||||
target,
|
||||
annotation,
|
||||
value: Some(value),
|
||||
..
|
||||
}),
|
||||
] if value.range() == expr.range() => match target.as_ref() {
|
||||
Expr::Name(name) => {
|
||||
format!(
|
||||
"{var}: {ann} = {binding}({filename_code}).{suggestion}",
|
||||
var = name.id,
|
||||
ann = locator.slice(annotation.range())
|
||||
)
|
||||
[Stmt::AnnAssign(ann_assign_node)] if ann_assign_node.value.as_ref().is_some_and(|v| v.range() == expr.range()) => {
|
||||
match ann_assign_node.target.as_ref() {
|
||||
Expr::Name(name) => {
|
||||
format!(
|
||||
"{var}: {ann} = {binding}({filename_code}).{suggestion}",
|
||||
var = name.id,
|
||||
ann = locator.slice(ann_assign_node.annotation.range())
|
||||
)
|
||||
}
|
||||
_ => return None,
|
||||
}
|
||||
_ => return None,
|
||||
},
|
||||
}
|
||||
_ => return None,
|
||||
};
|
||||
|
||||
|
||||
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user