Compare commits

...

7 Commits

Author SHA1 Message Date
konstin
6bd83d36ee Replace num_bigint with malachite
[malachite](https://www.malachite.rs/) is an arbitrary precision crate that unlike num_bigint implement as small integer optimization. This avoids allocating a `Vec` for every single number parse and instead only allocates for rare unusually large numbers.

This is also a correctness improvement since we'd previously just ignore the higher parts of unreasonably large numbers.

The disadvantage is that malachite is slow to compile.
2023-09-19 12:02:32 +02:00
konsti
94b68f201b Fix stylist indentation with a formfeed (#7489)
**Summary** In python, a formfeed is technically undefined behaviour
(https://docs.python.org/3/reference/lexical_analysis.html#indentation):
> A formfeed character may be present at the start of the line; it will
be ignored for
> the indentation calculations above. Formfeed characters occurring
elsewhere in the
> leading whitespace have an undefined effect (for instance, they may
reset the space
> count to zero).

In practice, they just reset the indentation:


df8b3a46a7/Parser/tokenizer.c (L1819-L1821)
a41bb2733f/crates/ruff_python_parser/src/lexer.rs (L664-L667)

The stylist didn't handle formfeeds previously and would produce invalid
indents. The remedy is to cut everything before a form feed.

Checks box for
https://github.com/astral-sh/ruff/issues/7455#issuecomment-1722458825

**Test Plan** Unit test for the stylist and a regression test for the
rule
2023-09-19 12:01:16 +02:00
konsti
ef34c5cbec Update itertools to 0.11 (#7513)
Preparation for #7469.

Changelog:
https://github.com/rust-itertools/itertools/blob/master/CHANGELOG.md#0110
2023-09-19 09:53:14 +00:00
dependabot[bot]
fdbefd777c Bump syn from 2.0.33 to 2.0.37 (#7512)
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
2023-09-19 11:08:56 +02:00
dependabot[bot]
078547adbb Bump clap from 4.4.3 to 4.4.4 (#7511)
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
2023-09-19 11:08:39 +02:00
dependabot[bot]
42a0bec146 Bump schemars from 0.8.13 to 0.8.15 (#7510)
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
2023-09-19 11:08:22 +02:00
Micha Reiser
37b7d0f921 fix: Compiler warning about unused map_or (#7508) 2023-09-19 08:10:01 +00:00
32 changed files with 383 additions and 221 deletions

255
Cargo.lock generated
View File

@@ -172,6 +172,12 @@ version = "1.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d468802bab17cbc0cc575e9b053f41e72aa36bfa6b7f55e3529ffa43161b97fa"
[[package]]
name = "base64"
version = "0.13.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9e1b586273c5702936fe7b7d6896644d8be71e6314cfe09d3167c95f712589e8"
[[package]]
name = "base64"
version = "0.21.3"
@@ -214,6 +220,15 @@ version = "2.4.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b4682ae6287fcf752ecaabbfcc7b6f9b72aa33933dc23a554d853aea8eea8635"
[[package]]
name = "block-buffer"
version = "0.10.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3078c7629b62d3f0439517fa394996acacc5cbc91c5a20d8c658e77abd503a71"
dependencies = [
"generic-array",
]
[[package]]
name = "bstr"
version = "1.6.2"
@@ -313,9 +328,9 @@ dependencies = [
[[package]]
name = "clap"
version = "4.4.3"
version = "4.4.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "84ed82781cea27b43c9b106a979fe450a13a31aab0500595fb3fc06616de08e6"
checksum = "b1d7b8d5ec32af0fadc644bf1fd509a688c2103b185644bb1e29d164e0703136"
dependencies = [
"clap_builder",
"clap_derive",
@@ -323,9 +338,9 @@ dependencies = [
[[package]]
name = "clap_builder"
version = "4.4.2"
version = "4.4.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "2bb9faaa7c2ef94b2743a21f5a29e6f0010dff4caa69ac8e9d6cf8b6fa74da08"
checksum = "5179bb514e4d7c2051749d8fcefa2ed6d06a9f4e6d69faf3805f5d80b8cf8d56"
dependencies = [
"anstream",
"anstyle",
@@ -383,7 +398,7 @@ dependencies = [
"heck",
"proc-macro2",
"quote",
"syn 2.0.33",
"syn 2.0.37",
]
[[package]]
@@ -495,6 +510,15 @@ version = "3.0.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7704b5fdd17b18ae31c4c1da5a2e0305a2bf17b5249300a9ee9ed7b72114c636"
[[package]]
name = "cpufeatures"
version = "0.2.9"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a17b76ff3a4162b0b27f354a0c87015ddad39d35f9c0c36607a3bdd175dde1f1"
dependencies = [
"libc",
]
[[package]]
name = "crc32fast"
version = "1.3.2"
@@ -516,7 +540,7 @@ dependencies = [
"clap",
"criterion-plot",
"is-terminal",
"itertools",
"itertools 0.10.5",
"num-traits",
"once_cell",
"oorandom",
@@ -535,7 +559,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "6b50826342786a51a89e2da3a28f1c32b06e387201bc2d19791f622c673706b1"
dependencies = [
"cast",
"itertools",
"itertools 0.10.5",
]
[[package]]
@@ -587,6 +611,16 @@ version = "0.2.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7a81dae078cea95a014a339291cec439d2f232ebe854a9d672b796c6afafa9b7"
[[package]]
name = "crypto-common"
version = "0.1.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1bfb12502f3fc46cca1bb51ac28df9d618d813cdc3d2f25b9fe775a34af26bb3"
dependencies = [
"generic-array",
"typenum",
]
[[package]]
name = "darling"
version = "0.20.3"
@@ -608,7 +642,7 @@ dependencies = [
"proc-macro2",
"quote",
"strsim",
"syn 2.0.33",
"syn 2.0.37",
]
[[package]]
@@ -619,7 +653,7 @@ checksum = "836a9bbc7ad63342d6d6e7b815ccab164bc77a2d95d84bc3117a8c0d5c98e2d5"
dependencies = [
"darling_core",
"quote",
"syn 2.0.33",
"syn 2.0.37",
]
[[package]]
@@ -634,6 +668,16 @@ version = "0.4.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "6184e33543162437515c2e2b48714794e37845ec9851711914eec9d308f6ebe8"
[[package]]
name = "digest"
version = "0.10.7"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9ed9a281f7bc9b7576e61468ba615a66a5c8cfdff42420a70aa82701a3b1e292"
dependencies = [
"block-buffer",
"crypto-common",
]
[[package]]
name = "dirs"
version = "4.0.0"
@@ -720,6 +764,18 @@ version = "1.9.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a26ae43d7bcc3b814de94796a5e736d4029efb0ee900c12e2d54c993ad1a1e07"
[[package]]
name = "embed-doc-image"
version = "0.1.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "af36f591236d9d822425cb6896595658fa558fcebf5ee8accac1d4b92c47166e"
dependencies = [
"base64 0.13.1",
"proc-macro2",
"quote",
"syn 1.0.109",
]
[[package]]
name = "ena"
version = "0.14.2"
@@ -816,7 +872,7 @@ dependencies = [
"clap",
"colored",
"configparser",
"itertools",
"itertools 0.11.0",
"log",
"once_cell",
"pep440_rs",
@@ -872,6 +928,16 @@ dependencies = [
"libc",
]
[[package]]
name = "generic-array"
version = "0.14.7"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "85649ca51fd72272d7821adaf274ad91c288277713d9c18820d8499a7ff69e9a"
dependencies = [
"typenum",
"version_check",
]
[[package]]
name = "getrandom"
version = "0.2.10"
@@ -1120,7 +1186,7 @@ dependencies = [
"pmutil 0.6.1",
"proc-macro2",
"quote",
"syn 2.0.33",
"syn 2.0.37",
]
[[package]]
@@ -1143,6 +1209,15 @@ dependencies = [
"either",
]
[[package]]
name = "itertools"
version = "0.11.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b1c173a5686ce8bfa551b3563d0c2170bf24ca44da99c7ca4bfdab5418c3fe57"
dependencies = [
"either",
]
[[package]]
name = "itoa"
version = "1.0.9"
@@ -1158,6 +1233,15 @@ dependencies = [
"wasm-bindgen",
]
[[package]]
name = "keccak"
version = "0.1.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8f6d5ed8676d904364de097082f4e7d240b571b67989ced0240f08b7f966f940"
dependencies = [
"cpufeatures",
]
[[package]]
name = "kqueue"
version = "1.0.8"
@@ -1189,7 +1273,7 @@ dependencies = [
"diff",
"ena",
"is-terminal",
"itertools",
"itertools 0.10.5",
"lalrpop-util",
"petgraph",
"regex",
@@ -1311,6 +1395,41 @@ version = "0.4.20"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b5e6163cb8c49088c2c36f57875e58ccd8c87c7427f7fbd50ea6710b2f3f2e8f"
[[package]]
name = "malachite"
version = "0.4.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a9fa232412d927f518cd873073911726943f432bac1bbc1288316d240635dc51"
dependencies = [
"malachite-base",
"malachite-nz",
]
[[package]]
name = "malachite-base"
version = "0.4.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "606a61b226dc58b8b283399b74754460433c193b193f26eaaad92f7966abd72b"
dependencies = [
"getrandom",
"itertools 0.11.0",
"rand",
"rand_chacha",
"ryu",
"sha3",
]
[[package]]
name = "malachite-nz"
version = "0.4.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "863d06218c83cc2269c425186cc15094d6284b1333a1184d0890aecfddb8916b"
dependencies = [
"embed-doc-image",
"itertools 0.11.0",
"malachite-base",
]
[[package]]
name = "matchers"
version = "0.1.0"
@@ -1445,27 +1564,6 @@ dependencies = [
"winapi",
]
[[package]]
name = "num-bigint"
version = "0.4.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "608e7659b5c3d7cba262d894801b9ec9d00de989e8a82bd4bef91d08da45cdc0"
dependencies = [
"autocfg",
"num-integer",
"num-traits",
]
[[package]]
name = "num-integer"
version = "0.1.45"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "225d3389fb3509a24c93f5c29eb6bde2586b98d9f016636dff58d7c6f7569cd9"
dependencies = [
"autocfg",
"num-traits",
]
[[package]]
name = "num-traits"
version = "0.2.16"
@@ -1720,7 +1818,7 @@ checksum = "52a40bc70c2c58040d2d8b167ba9a5ff59fc9dab7ad44771cfde3dcfde7a09c6"
dependencies = [
"proc-macro2",
"quote",
"syn 2.0.33",
"syn 2.0.37",
]
[[package]]
@@ -1749,7 +1847,7 @@ checksum = "09963355b9f467184c04017ced4a2ba2d75cbcb4e7462690d388233253d4b1a9"
dependencies = [
"anstyle",
"difflib",
"itertools",
"itertools 0.10.5",
"predicates-core",
]
@@ -2035,12 +2133,12 @@ dependencies = [
"imperative",
"insta",
"is-macro",
"itertools",
"itertools 0.11.0",
"libcst",
"log",
"malachite",
"memchr",
"natord",
"num-bigint",
"num-traits",
"once_cell",
"path-absolutize",
@@ -2112,7 +2210,7 @@ dependencies = [
"filetime",
"glob",
"globset",
"itertools",
"itertools 0.11.0",
"regex",
"ruff_macros",
]
@@ -2139,7 +2237,7 @@ dependencies = [
"insta",
"insta-cmd",
"is-macro",
"itertools",
"itertools 0.11.0",
"itoa",
"log",
"mimalloc",
@@ -2186,7 +2284,7 @@ dependencies = [
"imara-diff",
"indicatif",
"indoc",
"itertools",
"itertools 0.11.0",
"libcst",
"once_cell",
"pretty_assertions",
@@ -2255,11 +2353,11 @@ dependencies = [
name = "ruff_macros"
version = "0.0.0"
dependencies = [
"itertools",
"itertools 0.11.0",
"proc-macro2",
"quote",
"ruff_python_trivia",
"syn 2.0.33",
"syn 2.0.37",
]
[[package]]
@@ -2268,7 +2366,7 @@ version = "0.0.0"
dependencies = [
"anyhow",
"insta",
"itertools",
"itertools 0.11.0",
"once_cell",
"ruff_diagnostics",
"ruff_source_file",
@@ -2288,10 +2386,9 @@ dependencies = [
"bitflags 2.4.0",
"insta",
"is-macro",
"itertools",
"itertools 0.11.0",
"malachite",
"memchr",
"num-bigint",
"num-traits",
"once_cell",
"ruff_python_parser",
"ruff_python_trivia",
@@ -2323,7 +2420,7 @@ dependencies = [
"clap",
"countme",
"insta",
"itertools",
"itertools 0.11.0",
"memchr",
"once_cell",
"ruff_formatter",
@@ -2348,7 +2445,7 @@ dependencies = [
name = "ruff_python_index"
version = "0.0.0"
dependencies = [
"itertools",
"itertools 0.11.0",
"ruff_python_ast",
"ruff_python_parser",
"ruff_python_trivia",
@@ -2363,7 +2460,7 @@ dependencies = [
"bitflags 2.4.0",
"hexf-parse",
"is-macro",
"itertools",
"itertools 0.11.0",
"lexical-parse-float",
"num-traits",
"rand",
@@ -2377,11 +2474,10 @@ dependencies = [
"anyhow",
"insta",
"is-macro",
"itertools",
"itertools 0.11.0",
"lalrpop",
"lalrpop-util",
"num-bigint",
"num-traits",
"malachite",
"ruff_python_ast",
"ruff_text_size",
"rustc-hash",
@@ -2407,7 +2503,6 @@ version = "0.0.0"
dependencies = [
"bitflags 2.4.0",
"is-macro",
"num-traits",
"ruff_index",
"ruff_python_ast",
"ruff_python_parser",
@@ -2430,7 +2525,7 @@ name = "ruff_python_trivia"
version = "0.0.0"
dependencies = [
"insta",
"itertools",
"itertools 0.11.0",
"ruff_python_ast",
"ruff_python_parser",
"ruff_source_file",
@@ -2511,7 +2606,7 @@ dependencies = [
"glob",
"globset",
"ignore",
"itertools",
"itertools 0.11.0",
"log",
"path-absolutize",
"pep440_rs",
@@ -2612,9 +2707,9 @@ dependencies = [
[[package]]
name = "schemars"
version = "0.8.13"
version = "0.8.15"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "763f8cd0d4c71ed8389c90cb8100cba87e763bd01a8e614d4f0af97bcd50a161"
checksum = "1f7b0ce13155372a76ee2e1c5ffba1fe61ede73fbea5630d61eee6fac4929c0c"
dependencies = [
"dyn-clone",
"schemars_derive",
@@ -2624,9 +2719,9 @@ dependencies = [
[[package]]
name = "schemars_derive"
version = "0.8.13"
version = "0.8.15"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ec0f696e21e10fa546b7ffb1c9672c6de8fbc7a81acf59524386d8639bf12737"
checksum = "e85e2a16b12bdb763244c69ab79363d71db2b4b918a2def53f80b02e0574b13c"
dependencies = [
"proc-macro2",
"quote",
@@ -2690,7 +2785,7 @@ checksum = "4eca7ac642d82aa35b60049a6eccb4be6be75e599bd2e9adb5f875a737654af2"
dependencies = [
"proc-macro2",
"quote",
"syn 2.0.33",
"syn 2.0.37",
]
[[package]]
@@ -2752,7 +2847,17 @@ dependencies = [
"darling",
"proc-macro2",
"quote",
"syn 2.0.33",
"syn 2.0.37",
]
[[package]]
name = "sha3"
version = "0.10.8"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "75872d278a8f37ef87fa0ddbda7802605cb18344497949862c0d4dcb291eba60"
dependencies = [
"digest",
"keccak",
]
[[package]]
@@ -2847,7 +2952,7 @@ dependencies = [
"proc-macro2",
"quote",
"rustversion",
"syn 2.0.33",
"syn 2.0.37",
]
[[package]]
@@ -2863,9 +2968,9 @@ dependencies = [
[[package]]
name = "syn"
version = "2.0.33"
version = "2.0.37"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9caece70c63bfba29ec2fed841a09851b14a235c60010fa4de58089b6c025668"
checksum = "7303ef2c05cd654186cb250d29049a24840ca25d2747c25c0381c8d9e2f582e8"
dependencies = [
"proc-macro2",
"quote",
@@ -2952,7 +3057,7 @@ dependencies = [
"proc-macro-error",
"proc-macro2",
"quote",
"syn 2.0.33",
"syn 2.0.37",
]
[[package]]
@@ -2964,7 +3069,7 @@ dependencies = [
"proc-macro-error",
"proc-macro2",
"quote",
"syn 2.0.33",
"syn 2.0.37",
"test-case-core",
]
@@ -2985,7 +3090,7 @@ checksum = "49922ecae66cc8a249b77e68d1d0623c1b2c514f0060c27cdc68bd62a1219d35"
dependencies = [
"proc-macro2",
"quote",
"syn 2.0.33",
"syn 2.0.37",
]
[[package]]
@@ -3107,7 +3212,7 @@ checksum = "5f4f31f56159e98206da9efd823404b79b6ef3143b4a7ab76e67b1751b25a4ab"
dependencies = [
"proc-macro2",
"quote",
"syn 2.0.33",
"syn 2.0.37",
]
[[package]]
@@ -3167,6 +3272,12 @@ version = "2.0.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "6af6ae20167a9ece4bcb41af5b80f8a1f1df981f6391189ce00fd257af04126a"
[[package]]
name = "typenum"
version = "1.17.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "42ff0bf0c66b8238c6f3b578df37d0b7848e55df8577b3f74f92a69acceeb825"
[[package]]
name = "unic-char-property"
version = "0.9.0"
@@ -3262,7 +3373,7 @@ version = "2.7.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "0b11c96ac7ee530603dcdf68ed1557050f374ce55a5a07193ebf8cbc9f8927e9"
dependencies = [
"base64",
"base64 0.21.3",
"flate2",
"log",
"once_cell",
@@ -3310,7 +3421,7 @@ checksum = "f7e1ba1f333bd65ce3c9f27de592fcbc256dafe3af2717f56d7c87761fbaccf4"
dependencies = [
"proc-macro2",
"quote",
"syn 2.0.33",
"syn 2.0.37",
]
[[package]]
@@ -3404,7 +3515,7 @@ dependencies = [
"once_cell",
"proc-macro2",
"quote",
"syn 2.0.33",
"syn 2.0.37",
"wasm-bindgen-shared",
]
@@ -3438,7 +3549,7 @@ checksum = "54681b18a46765f095758388f2d0cf16eb8d4169b639ab575a8f5693af210c7b"
dependencies = [
"proc-macro2",
"quote",
"syn 2.0.33",
"syn 2.0.37",
"wasm-bindgen-backend",
"wasm-bindgen-shared",
]

View File

@@ -15,7 +15,7 @@ license = "MIT"
anyhow = { version = "1.0.69" }
bitflags = { version = "2.3.1" }
chrono = { version = "0.4.31", default-features = false, features = ["clock"] }
clap = { version = "4.4.3", features = ["derive"] }
clap = { version = "4.4.4", features = ["derive"] }
colored = { version = "2.0.0" }
filetime = { version = "0.2.20" }
glob = { version = "0.3.1" }
@@ -23,10 +23,10 @@ globset = { version = "0.4.10" }
ignore = { version = "0.4.20" }
insta = { version = "1.31.0", feature = ["filters", "glob"] }
is-macro = { version = "0.3.0" }
itertools = { version = "0.10.5" }
itertools = { version = "0.11.0" }
log = { version = "0.4.17" }
malachite = { version = "0.4.0", default-features = false, features = ["naturals_and_integers"] }
memchr = "2.6.3"
num-bigint = { version = "0.4.3" }
num-traits = { version = "0.2.15" }
once_cell = { version = "1.17.1" }
path-absolutize = { version = "3.1.1" }
@@ -34,7 +34,7 @@ proc-macro2 = { version = "1.0.67" }
quote = { version = "1.0.23" }
regex = { version = "1.9.5" }
rustc-hash = { version = "1.1.0" }
schemars = { version = "0.8.12" }
schemars = { version = "0.8.15" }
serde = { version = "1.0.152", features = ["derive"] }
serde_json = { version = "1.0.107" }
shellexpand = { version = "3.0.0" }
@@ -43,7 +43,7 @@ smallvec = { version = "1.10.0" }
static_assertions = "1.1.0"
strum = { version = "0.25.0", features = ["strum_macros"] }
strum_macros = { version = "0.25.2" }
syn = { version = "2.0.33" }
syn = { version = "2.0.37" }
test-case = { version = "3.2.1" }
thiserror = { version = "1.0.48" }
toml = { version = "0.7.8" }

View File

@@ -45,9 +45,9 @@ is-macro = { workspace = true }
itertools = { workspace = true }
libcst = { workspace = true }
log = { workspace = true }
malachite = { workspace = true }
memchr = { workspace = true }
natord = { version = "1.0.9" }
num-bigint = { workspace = true }
num-traits = { workspace = true }
once_cell = { workspace = true }
path-absolutize = { workspace = true, features = [

View File

@@ -0,0 +1,9 @@
# formfeed indent
# https://github.com/astral-sh/ruff/issues/7455#issuecomment-1722458825
# This is technically a stylist bug (and has a test there), but it surfaced in B006
class FormFeedIndent:
def __init__(self, a=[]):
print(a)

View File

@@ -1,8 +1,6 @@
use num_bigint::BigInt;
use ruff_python_ast::{self as ast, CmpOp, Constant, Expr};
use ruff_diagnostics::{Diagnostic, Violation};
use ruff_macros::{derive_message_formats, violation};
use ruff_python_ast::{self as ast, CmpOp, Constant, Expr};
use ruff_text_size::Ranged;
use crate::checkers::ast::Checker;
@@ -237,7 +235,7 @@ pub(crate) fn compare(checker: &mut Checker, left: &Expr, ops: &[CmpOp], compara
..
}) = slice.as_ref()
{
if *i == BigInt::from(0) {
if *i == 0 {
if let (
[CmpOp::Eq | CmpOp::NotEq],
[Expr::Constant(ast::ExprConstant {
@@ -246,13 +244,13 @@ pub(crate) fn compare(checker: &mut Checker, left: &Expr, ops: &[CmpOp], compara
})],
) = (ops, comparators)
{
if *n == BigInt::from(3) && checker.enabled(Rule::SysVersionInfo0Eq3) {
if *n == 3 && checker.enabled(Rule::SysVersionInfo0Eq3) {
checker
.diagnostics
.push(Diagnostic::new(SysVersionInfo0Eq3, left.range()));
}
}
} else if *i == BigInt::from(1) {
} else if *i == 1 {
if let (
[CmpOp::Lt | CmpOp::LtE | CmpOp::Gt | CmpOp::GtE],
[Expr::Constant(ast::ExprConstant {

View File

@@ -1,4 +1,3 @@
use num_bigint::BigInt;
use ruff_python_ast::{self as ast, Constant, Expr};
use ruff_diagnostics::{Diagnostic, Violation};
@@ -184,11 +183,11 @@ pub(crate) fn subscript(checker: &mut Checker, value: &Expr, slice: &Expr) {
..
}) = upper.as_ref()
{
if *i == BigInt::from(1) && checker.enabled(Rule::SysVersionSlice1) {
if *i == 1 && checker.enabled(Rule::SysVersionSlice1) {
checker
.diagnostics
.push(Diagnostic::new(SysVersionSlice1, value.range()));
} else if *i == BigInt::from(3) && checker.enabled(Rule::SysVersionSlice3) {
} else if *i == 3 && checker.enabled(Rule::SysVersionSlice3) {
checker
.diagnostics
.push(Diagnostic::new(SysVersionSlice3, value.range()));
@@ -200,11 +199,11 @@ pub(crate) fn subscript(checker: &mut Checker, value: &Expr, slice: &Expr) {
value: Constant::Int(i),
..
}) => {
if *i == BigInt::from(2) && checker.enabled(Rule::SysVersion2) {
if *i == 2 && checker.enabled(Rule::SysVersion2) {
checker
.diagnostics
.push(Diagnostic::new(SysVersion2, value.range()));
} else if *i == BigInt::from(0) && checker.enabled(Rule::SysVersion0) {
} else if *i == 0 && checker.enabled(Rule::SysVersion0) {
checker
.diagnostics
.push(Diagnostic::new(SysVersion0, value.range()));

View File

@@ -1,4 +1,4 @@
use num_traits::ToPrimitive;
use malachite::Integer;
use ruff_diagnostics::{Diagnostic, Violation};
use ruff_macros::{derive_message_formats, violation};
@@ -36,7 +36,7 @@ use crate::checkers::ast::Checker;
/// - [Common Weakness Enumeration: CWE-732](https://cwe.mitre.org/data/definitions/732.html)
#[violation]
pub struct BadFilePermissions {
mask: u16,
mask: Integer,
}
impl Violation for BadFilePermissions {
@@ -56,7 +56,9 @@ pub(crate) fn bad_file_permissions(checker: &mut Checker, call: &ast::ExprCall)
{
if let Some(mode_arg) = call.arguments.find_argument("mode", 1) {
if let Some(int_value) = int_value(mode_arg, checker.semantic()) {
if (int_value & WRITE_WORLD > 0) || (int_value & EXECUTE_GROUP > 0) {
if (int_value.clone() & Integer::from(WRITE_WORLD) > 0)
|| (int_value.clone() & Integer::from(EXECUTE_GROUP) > 0)
{
checker.diagnostics.push(Diagnostic::new(
BadFilePermissions { mask: int_value },
mode_arg.range(),
@@ -113,13 +115,17 @@ fn py_stat(call_path: &CallPath) -> Option<u16> {
}
}
fn int_value(expr: &Expr, semantic: &SemanticModel) -> Option<u16> {
fn int_value(expr: &Expr, semantic: &SemanticModel) -> Option<Integer> {
match expr {
Expr::Constant(ast::ExprConstant {
value: Constant::Int(value),
..
}) => value.to_u16(),
Expr::Attribute(_) => semantic.resolve_call_path(expr).as_ref().and_then(py_stat),
}) => Some(value.clone()),
Expr::Attribute(_) => semantic
.resolve_call_path(expr)
.as_ref()
.and_then(py_stat)
.map(Integer::from),
Expr::BinOp(ast::ExprBinOp {
left,
op,

View File

@@ -67,26 +67,23 @@ pub(crate) fn flask_debug_true(checker: &mut Checker, call: &ExprCall) {
return;
};
checker
.semantic()
.resolve_name(name)
.map_or((), |binding_id| {
if let Some(Stmt::Assign(StmtAssign { value, .. })) = checker
.semantic()
.binding(binding_id)
.statement(checker.semantic())
{
if let Expr::Call(ExprCall { func, .. }) = value.as_ref() {
if checker
.semantic()
.resolve_call_path(func)
.is_some_and(|call_path| matches!(call_path.as_slice(), ["flask", "Flask"]))
{
checker
.diagnostics
.push(Diagnostic::new(FlaskDebugTrue, debug_argument.range()));
}
if let Some(binding_id) = checker.semantic().resolve_name(name) {
if let Some(Stmt::Assign(StmtAssign { value, .. })) = checker
.semantic()
.binding(binding_id)
.statement(checker.semantic())
{
if let Expr::Call(ExprCall { func, .. }) = value.as_ref() {
if checker
.semantic()
.resolve_call_path(func)
.is_some_and(|call_path| matches!(call_path.as_slice(), ["flask", "Flask"]))
{
checker
.diagnostics
.push(Diagnostic::new(FlaskDebugTrue, debug_argument.range()));
}
}
});
}
};
}

View File

@@ -1,5 +1,3 @@
use num_traits::{One, Zero};
use ruff_diagnostics::{Diagnostic, Violation};
use ruff_macros::{derive_message_formats, violation};
use ruff_python_ast::{self as ast, Constant, Expr};
@@ -57,7 +55,7 @@ pub(crate) fn snmp_insecure_version(checker: &mut Checker, call: &ast::ExprCall)
..
}) = &keyword.value
{
if value.is_zero() || value.is_one() {
if *value == 0 || *value == 1 {
checker
.diagnostics
.push(Diagnostic::new(SnmpInsecureVersion, keyword.range()));

View File

@@ -36,6 +36,7 @@ mod tests {
#[test_case(Rule::MutableArgumentDefault, Path::new("B006_1.py"))]
#[test_case(Rule::MutableArgumentDefault, Path::new("B006_2.py"))]
#[test_case(Rule::MutableArgumentDefault, Path::new("B006_3.py"))]
#[test_case(Rule::MutableArgumentDefault, Path::new("B006_4.py"))]
#[test_case(Rule::NoExplicitStacklevel, Path::new("B028.py"))]
#[test_case(Rule::RaiseLiteral, Path::new("B016.py"))]
#[test_case(Rule::RaiseWithoutFromInsideExcept, Path::new("B904.py"))]

View File

@@ -0,0 +1,24 @@
---
source: crates/ruff/src/rules/flake8_bugbear/mod.rs
---
B006_4.py:7:26: B006 [*] Do not use mutable data structures for argument defaults
|
6 | class FormFeedIndent:
7 | def __init__(self, a=[]):
| ^^ B006
8 | print(a)
|
= help: Replace with `None`; initialize within function
Possible fix
4 4 |
5 5 |
6 6 | class FormFeedIndent:
7 |- def __init__(self, a=[]):
7 |+ def __init__(self, a=None):
8 |+ if a is None:
9 |+ a = []
8 10 | print(a)
9 11 |

View File

@@ -1,5 +1,3 @@
use num_bigint::BigInt;
use ruff_diagnostics::{Diagnostic, Violation};
use ruff_macros::{derive_message_formats, violation};
use ruff_python_ast::{self as ast, Constant, Expr, UnaryOp};
@@ -93,7 +91,7 @@ pub(crate) fn unnecessary_subscript_reversal(
else {
return;
};
if *val != BigInt::from(1) {
if *val != 1 {
return;
};
checker.diagnostics.push(Diagnostic::new(

View File

@@ -1,5 +1,3 @@
use num_bigint::BigInt;
use ruff_diagnostics::Diagnostic;
use ruff_diagnostics::{AlwaysAutofixableViolation, Fix};
use ruff_macros::{derive_message_formats, violation};
@@ -75,7 +73,7 @@ pub(crate) fn unnecessary_range_start(checker: &mut Checker, call: &ast::ExprCal
else {
return;
};
if *value != BigInt::from(0) {
if *value != 0 {
return;
};

View File

@@ -1,5 +1,3 @@
use num_bigint::BigInt;
use num_traits::{One, Zero};
use ruff_python_ast::{self as ast, CmpOp, Constant, Expr};
use ruff_diagnostics::{Diagnostic, Violation};
@@ -249,10 +247,10 @@ impl ExpectedComparator {
..
}) = upper.as_ref()
{
if *upper == BigInt::one() {
if *upper == 1 {
return Some(ExpectedComparator::MajorTuple);
}
if *upper == BigInt::from(2) {
if *upper == 2 {
return Some(ExpectedComparator::MajorMinorTuple);
}
}
@@ -260,7 +258,7 @@ impl ExpectedComparator {
Expr::Constant(ast::ExprConstant {
value: Constant::Int(n),
..
}) if n.is_zero() => {
}) if *n == 0 => {
return Some(ExpectedComparator::MajorDigit);
}
_ => (),

View File

@@ -1,4 +1,3 @@
use num_traits::One;
use ruff_python_ast::{self as ast, CmpOp, Constant, Expr};
use ruff_diagnostics::Diagnostic;
@@ -115,7 +114,7 @@ pub(crate) fn nunique_constant_series_check(
fn is_constant_one(expr: &Expr) -> bool {
match expr {
Expr::Constant(constant) => match &constant.value {
Constant::Int(int) => int.is_one(),
Constant::Int(int) => *int == 1,
_ => false,
},
_ => false,

View File

@@ -1,8 +1,7 @@
use malachite::Integer;
use std::fmt;
use std::str::FromStr;
use num_bigint::BigInt;
use ruff_diagnostics::{AlwaysAutofixableViolation, Diagnostic, Edit, Fix};
use ruff_macros::{derive_message_formats, violation};
use ruff_python_ast::{self as ast, Constant, Expr};
@@ -47,7 +46,7 @@ impl From<LiteralType> for Constant {
value: Vec::new(),
implicit_concatenated: false,
}),
LiteralType::Int => Constant::Int(BigInt::from(0)),
LiteralType::Int => Constant::Int(Integer::from(0)),
LiteralType::Float => Constant::Float(0.0),
LiteralType::Bool => Constant::Bool(false),
}

View File

@@ -1,6 +1,6 @@
use std::cmp::Ordering;
use num_bigint::{BigInt, Sign};
use malachite::Integer;
use ruff_diagnostics::{AlwaysAutofixableViolation, Diagnostic, Edit, Fix};
use ruff_macros::{derive_message_formats, violation};
@@ -122,31 +122,24 @@ pub(crate) fn outdated_version_block(checker: &mut Checker, stmt_if: &StmtIf) {
..
}) => {
if op == &CmpOp::Eq {
match bigint_to_u32(number) {
2 => {
let mut diagnostic =
Diagnostic::new(OutdatedVersionBlock, branch.test.range());
if checker.patch(diagnostic.kind.rule()) {
if let Some(fix) =
fix_always_false_branch(checker, stmt_if, &branch)
{
diagnostic.set_fix(fix);
}
if *number == 2 {
let mut diagnostic =
Diagnostic::new(OutdatedVersionBlock, branch.test.range());
if checker.patch(diagnostic.kind.rule()) {
if let Some(fix) = fix_always_false_branch(checker, stmt_if, &branch) {
diagnostic.set_fix(fix);
}
checker.diagnostics.push(diagnostic);
}
3 => {
let mut diagnostic =
Diagnostic::new(OutdatedVersionBlock, branch.test.range());
if checker.patch(diagnostic.kind.rule()) {
if let Some(fix) = fix_always_true_branch(checker, stmt_if, &branch)
{
diagnostic.set_fix(fix);
}
checker.diagnostics.push(diagnostic);
} else if *number == 3 {
let mut diagnostic =
Diagnostic::new(OutdatedVersionBlock, branch.test.range());
if checker.patch(diagnostic.kind.rule()) {
if let Some(fix) = fix_always_true_branch(checker, stmt_if, &branch) {
diagnostic.set_fix(fix);
}
checker.diagnostics.push(diagnostic);
}
_ => {}
checker.diagnostics.push(diagnostic);
}
}
}
@@ -156,7 +149,7 @@ pub(crate) fn outdated_version_block(checker: &mut Checker, stmt_if: &StmtIf) {
}
/// Returns true if the `target_version` is always less than the [`PythonVersion`].
fn compare_version(target_version: &[u32], py_version: PythonVersion, or_equal: bool) -> bool {
fn compare_version(target_version: &[Integer], py_version: PythonVersion, or_equal: bool) -> bool {
let mut target_version_iter = target_version.iter();
let Some(if_major) = target_version_iter.next() else {
@@ -165,7 +158,7 @@ fn compare_version(target_version: &[u32], py_version: PythonVersion, or_equal:
let (py_major, py_minor) = py_version.as_tuple();
match if_major.cmp(&py_major) {
match if_major.cmp(&Integer::from(py_major)) {
Ordering::Less => true,
Ordering::Greater => false,
Ordering::Equal => {
@@ -353,26 +346,16 @@ fn fix_always_true_branch(
}
}
/// Converts a `BigInt` to a `u32`. If the number is negative, it will return 0.
fn bigint_to_u32(number: &BigInt) -> u32 {
let the_number = number.to_u32_digits();
match the_number.0 {
Sign::Minus | Sign::NoSign => 0,
Sign::Plus => *the_number.1.first().unwrap(),
}
}
/// Gets the version from the tuple
fn extract_version(elts: &[Expr]) -> Vec<u32> {
let mut version: Vec<u32> = vec![];
fn extract_version(elts: &[Expr]) -> Vec<Integer> {
let mut version = Vec::new();
for elt in elts {
if let Expr::Constant(ast::ExprConstant {
value: Constant::Int(item),
value: Constant::Int(number),
..
}) = &elt
{
let number = bigint_to_u32(item);
version.push(number);
version.push(number.clone());
} else {
return version;
}
@@ -403,6 +386,13 @@ mod tests {
or_equal: bool,
expected: bool,
) {
assert_eq!(compare_version(version_vec, version, or_equal), expected);
let version_integers = version_vec
.iter()
.map(|x| Integer::from(*x))
.collect::<Vec<_>>();
assert_eq!(
compare_version(&version_integers, version, or_equal),
expected
);
}
}

View File

@@ -1,4 +1,4 @@
use num_traits::ToPrimitive;
use malachite::Integer;
use ruff_python_ast::{self as ast, Constant, Expr, UnaryOp};
use ruff_diagnostics::{Diagnostic, Violation};
@@ -46,11 +46,11 @@ impl Violation for PairwiseOverZipped {
#[derive(Debug)]
struct SliceInfo {
arg_name: String,
slice_start: Option<i64>,
slice_start: Option<Integer>,
}
impl SliceInfo {
pub(crate) fn new(arg_name: String, slice_start: Option<i64>) -> Self {
pub(crate) fn new(arg_name: String, slice_start: Option<Integer>) -> Self {
Self {
arg_name,
slice_start,
@@ -89,12 +89,12 @@ fn match_slice_info(expr: &Expr) -> Option<SliceInfo> {
))
}
fn to_bound(expr: &Expr) -> Option<i64> {
fn to_bound(expr: &Expr) -> Option<Integer> {
match expr {
Expr::Constant(ast::ExprConstant {
value: Constant::Int(value),
..
}) => value.to_i64(),
}) => Some(value.clone()),
Expr::UnaryOp(ast::ExprUnaryOp {
op: UnaryOp::USub | UnaryOp::Invert,
operand,
@@ -105,7 +105,7 @@ fn to_bound(expr: &Expr) -> Option<i64> {
..
}) = operand.as_ref()
{
value.to_i64().map(|v| -v)
Some(-value.clone())
} else {
None
}
@@ -155,7 +155,10 @@ pub(crate) fn pairwise_over_zipped(checker: &mut Checker, func: &Expr, args: &[E
}
// Verify that the arguments are successive.
if second_arg_info.slice_start.unwrap_or(0) - first_arg_info.slice_start.unwrap_or(0) != 1 {
if second_arg_info.slice_start.unwrap_or(Integer::from(0))
- first_arg_info.slice_start.unwrap_or(Integer::from(0))
!= 1
{
return;
}

View File

@@ -1,7 +1,5 @@
use std::borrow::Cow;
use num_traits::Zero;
use ruff_diagnostics::{AlwaysAutofixableViolation, Diagnostic, Edit, Fix};
use ruff_macros::{derive_message_formats, violation};
use ruff_python_ast::{self as ast, Arguments, Comprehension, Constant, Expr};
@@ -115,7 +113,7 @@ fn is_head_slice(expr: &Expr) -> bool {
..
}) = expr
{
value.is_zero()
*value == 0
} else {
false
}

View File

@@ -20,9 +20,8 @@ ruff_text_size = { path = "../ruff_text_size" }
bitflags = { workspace = true }
is-macro = { workspace = true }
itertools = { workspace = true }
malachite = { workspace = true }
memchr = { workspace = true }
num-bigint = { workspace = true }
num-traits = { workspace = true }
once_cell = { workspace = true }
rustc-hash = { workspace = true }
serde = { workspace = true, optional = true }

View File

@@ -15,9 +15,8 @@
//! an implicit concatenation of string literals, as these expressions are considered to
//! have the same shape in that they evaluate to the same value.
use num_bigint::BigInt;
use crate as ast;
use malachite::Integer;
#[derive(Debug, PartialEq, Eq, Hash, Copy, Clone)]
pub enum ComparableBoolOp {
@@ -334,7 +333,7 @@ pub enum ComparableConstant<'a> {
Bool(&'a bool),
Str { value: &'a str, unicode: bool },
Bytes(&'a [u8]),
Int(&'a BigInt),
Int(&'a Integer),
Tuple(Vec<ComparableConstant<'a>>),
Float(u64),
Complex { real: u64, imag: u64 },

View File

@@ -1,7 +1,6 @@
use std::borrow::Cow;
use std::path::Path;
use num_traits::Zero;
use smallvec::SmallVec;
use ruff_text_size::{Ranged, TextRange};
@@ -1073,7 +1072,7 @@ impl Truthiness {
Constant::None => Some(false),
Constant::Str(ast::StringConstant { value, .. }) => Some(!value.is_empty()),
Constant::Bytes(bytes) => Some(!bytes.is_empty()),
Constant::Int(int) => Some(!int.is_zero()),
Constant::Int(int) => Some(*int != 0),
Constant::Float(float) => Some(*float != 0.0),
Constant::Complex { real, imag } => Some(*real != 0.0 || *imag != 0.0),
Constant::Ellipsis => Some(true),

View File

@@ -1,12 +1,11 @@
#![allow(clippy::derive_partial_eq_without_eq)]
use itertools::Itertools;
use malachite::Integer;
use std::fmt;
use std::fmt::Debug;
use std::ops::Deref;
use num_bigint::BigInt;
use ruff_text_size::{Ranged, TextRange, TextSize};
/// See also [mod](https://docs.python.org/3/library/ast.html#ast.mod)
@@ -2604,7 +2603,7 @@ pub enum Constant {
Bool(bool),
Str(StringConstant),
Bytes(BytesConstant),
Int(BigInt),
Int(Integer),
Float(f64),
Complex { real: f64, imag: f64 },
Ellipsis,

View File

@@ -84,7 +84,21 @@ fn detect_indention(tokens: &[LexResult], locator: &Locator) -> Indentation {
});
if let Some(indent_range) = indent_range {
let whitespace = locator.slice(*indent_range);
let mut whitespace = locator.slice(*indent_range);
// https://docs.python.org/3/reference/lexical_analysis.html#indentation
// > A formfeed character may be present at the start of the line; it will be ignored for
// > the indentation calculations above. Formfeed characters occurring elsewhere in the
// > leading whitespace have an undefined effect (for instance, they may reset the space
// > count to zero).
// So there's UB in python lexer -.-
// In practice, they just reset the indentation:
// https://github.com/python/cpython/blob/df8b3a46a7aa369f246a09ffd11ceedf1d34e921/Parser/tokenizer.c#L1819-L1821
// https://github.com/astral-sh/ruff/blob/a41bb2733fe75a71f4cf6d4bb21e659fc4630b30/crates/ruff_python_parser/src/lexer.rs#L664-L667
// We also reset the indentation when we see a formfeed character.
// See also https://github.com/astral-sh/ruff/issues/7455#issuecomment-1722458825
if let Some((_before, after)) = whitespace.rsplit_once('\x0C') {
whitespace = after;
}
Indentation(whitespace.to_string())
} else {
@@ -228,6 +242,19 @@ x = (
Stylist::from_tokens(&tokens, &locator).indentation(),
&Indentation::default()
);
// formfeed indent, see `detect_indention` comment.
let contents = r#"
class FormFeedIndent:
def __init__(self, a=[]):
print(a)
"#;
let locator = Locator::new(contents);
let tokens: Vec<_> = lex(contents, Mode::Module).collect();
assert_eq!(
Stylist::from_tokens(&tokens, &locator).indentation(),
&Indentation(" ".to_string())
);
}
#[test]

View File

@@ -1,5 +1,5 @@
use crate::Case;
use num_traits::{Float, Zero};
use num_traits::Float;
use std::f64;
pub fn parse_str(literal: &str) -> Option<f64> {
@@ -248,7 +248,7 @@ pub fn to_hex(value: f64) -> String {
let (mantissa, exponent, sign) = value.integer_decode();
let sign_fmt = if sign < 0 { "-" } else { "" };
match value {
value if value.is_zero() => format!("{sign_fmt}0x0.0p+0"),
value if value == 0.0 => format!("{sign_fmt}0x0.0p+0"),
value if value.is_infinite() => format!("{sign_fmt}inf"),
value if value.is_nan() => "nan".to_owned(),
_ => {

View File

@@ -21,8 +21,7 @@ anyhow = { workspace = true }
is-macro = { workspace = true }
itertools = { workspace = true }
lalrpop-util = { version = "0.20.0", default-features = false }
num-bigint = { workspace = true }
num-traits = { workspace = true }
malachite = { workspace = true }
unicode-ident = { workspace = true }
unicode_names2 = { version = "0.6.0", git = "https://github.com/youknowone/unicode_names2.git", rev = "4ce16aa85cbcdd9cc830410f1a72ef9a235f2fde" }
rustc-hash = { workspace = true }

View File

@@ -28,11 +28,11 @@
//!
//! [Lexical analysis]: https://docs.python.org/3/reference/lexical_analysis.html
use malachite::num::conversion::traits::FromStringBase;
use malachite::Integer;
use std::iter::FusedIterator;
use std::{char, cmp::Ordering, str::FromStr};
use num_bigint::BigInt;
use num_traits::{Num, Zero};
use ruff_python_ast::IpyEscapeKind;
use ruff_text_size::{TextLen, TextRange, TextSize};
use unicode_ident::{is_xid_continue, is_xid_start};
@@ -264,11 +264,16 @@ impl<'source> Lexer<'source> {
let mut number = LexedText::new(self.offset(), self.source);
self.radix_run(&mut number, radix);
let value =
BigInt::from_str_radix(number.as_str(), radix.as_u32()).map_err(|e| LexicalError {
error: LexicalErrorType::OtherError(format!("{e:?}")),
let value = Integer::from_string_base(radix.as_u8(), number.as_str()).ok_or_else(|| {
LexicalError {
error: LexicalErrorType::OtherError(format!(
"'{}' is not a valid integer",
number.as_str()
)),
location: self.token_range().start(),
})?;
}
})?;
Ok(Tok::Int { value })
}
@@ -339,8 +344,19 @@ impl<'source> Lexer<'source> {
let imag = f64::from_str(number.as_str()).unwrap();
Ok(Tok::Complex { real: 0.0, imag })
} else {
let value = number.as_str().parse::<BigInt>().unwrap();
if start_is_zero && !value.is_zero() {
// Optimization: `Natural` and thereby `Integer` add up digits from bytes to convert
// from string. This is much less efficient than the highly optimized std
// string-to-number parser and we know that most numbers are small.
// i32::MAX is 2147483647 (10 digits), so all 9 digit numbers are representable as
// i32. We can't guarantee that `32_bit_limbs` isn't set, so we limit ourselves to
// i32 instead of i64.
let value = if number.as_str().len() < 10 {
Integer::from(i32::from_str(number.as_str()).unwrap())
} else {
Integer::from_str(number.as_str()).unwrap()
};
if start_is_zero && value != 0 {
// leading zeros in decimal integer literals are not permitted
return Err(LexicalError {
error: LexicalErrorType::OtherError("Invalid Token".to_owned()),
@@ -1155,7 +1171,7 @@ impl State {
}
}
#[derive(Copy, Clone, Debug)]
#[derive(Copy, Clone, Debug, PartialEq, Eq)]
enum Radix {
Binary,
Octal,
@@ -1164,7 +1180,7 @@ enum Radix {
}
impl Radix {
const fn as_u32(self) -> u32 {
const fn as_u8(self) -> u8 {
match self {
Radix::Binary => 2,
Radix::Octal => 8,

View File

@@ -3,7 +3,6 @@
// See also: file:///usr/share/doc/python/html/reference/compound_stmts.html#function-definitions
// See also: https://greentreesnakes.readthedocs.io/en/latest/nodes.html#keyword
use num_bigint::BigInt;
use ruff_text_size::{Ranged, TextSize};
use ruff_python_ast::{self as ast, IpyEscapeKind};
use crate::{
@@ -15,6 +14,7 @@ use crate::{
token::{self, StringKind},
};
use lalrpop_util::ParseError;
use malachite::Integer;
grammar(mode: Mode);
@@ -1928,7 +1928,7 @@ extern {
"True" => token::Tok::True,
"False" => token::Tok::False,
"None" => token::Tok::None,
int => token::Tok::Int { value: <BigInt> },
int => token::Tok::Int { value: <Integer> },
float => token::Tok::Float { value: <f64> },
complex => token::Tok::Complex { real: <f64>, imag: <f64> },
string => token::Tok::String {

View File

@@ -1,6 +1,5 @@
// auto-generated: "lalrpop 0.20.0"
// sha3: e8f3229288c1a13387ea6041355e2d8fe9ab788fbc7229032d2de92beb675944
use num_bigint::BigInt;
// sha3: 5b982370860d3a79f183bc579ec6f5165211b83573141f4c9ceaec27436c30ad
use ruff_text_size::{Ranged, TextSize};
use ruff_python_ast::{self as ast, IpyEscapeKind};
use crate::{
@@ -12,6 +11,7 @@ use crate::{
token::{self, StringKind},
};
use lalrpop_util::ParseError;
use malachite::Integer;
#[allow(unused_extern_crates)]
extern crate lalrpop_util as __lalrpop_util;
#[allow(unused_imports)]
@@ -23,7 +23,6 @@ extern crate alloc;
#[allow(non_snake_case, non_camel_case_types, unused_mut, unused_variables, unused_imports, unused_parens, clippy::all)]
mod __parse__Top {
use num_bigint::BigInt;
use ruff_text_size::{Ranged, TextSize};
use ruff_python_ast::{self as ast, IpyEscapeKind};
use crate::{
@@ -35,6 +34,7 @@ mod __parse__Top {
token::{self, StringKind},
};
use lalrpop_util::ParseError;
use malachite::Integer;
#[allow(unused_extern_crates)]
extern crate lalrpop_util as __lalrpop_util;
#[allow(unused_imports)]
@@ -48,7 +48,7 @@ mod __parse__Top {
Variant0(token::Tok),
Variant1((f64, f64)),
Variant2(f64),
Variant3(BigInt),
Variant3(Integer),
Variant4((IpyEscapeKind, String)),
Variant5(String),
Variant6((String, StringKind, bool)),
@@ -17716,7 +17716,7 @@ mod __parse__Top {
fn __pop_Variant3<
>(
__symbols: &mut alloc::vec::Vec<(TextSize,__Symbol<>,TextSize)>
) -> (TextSize, BigInt, TextSize)
) -> (TextSize, Integer, TextSize)
{
match __symbols.pop() {
Some((__l, __Symbol::Variant3(__v), __r)) => (__l, __v, __r),
@@ -34480,7 +34480,7 @@ fn __action232<
fn __action233<
>(
mode: Mode,
(_, value, _): (TextSize, BigInt, TextSize),
(_, value, _): (TextSize, Integer, TextSize),
) -> ast::Constant
{
ast::Constant::Int(value)

View File

@@ -5,7 +5,7 @@
//!
//! [CPython source]: https://github.com/python/cpython/blob/dfc2e065a2e71011017077e549cd2f9bf4944c54/Include/internal/pycore_token.h;
use crate::Mode;
use num_bigint::BigInt;
use malachite::Integer;
use ruff_python_ast::IpyEscapeKind;
use ruff_text_size::TextSize;
use std::fmt;
@@ -21,7 +21,7 @@ pub enum Tok {
/// Token value for an integer.
Int {
/// The integer value.
value: BigInt,
value: Integer,
},
/// Token value for a floating point number.
Float {

View File

@@ -21,7 +21,6 @@ ruff_text_size = { path = "../ruff_text_size" }
bitflags = { workspace = true }
is-macro = { workspace = true }
num-traits = { workspace = true }
rustc-hash = { workspace = true }
smallvec = { workspace = true }

View File

@@ -1,6 +1,5 @@
//! Analysis rules for the `typing` module.
use num_traits::identities::Zero;
use ruff_python_ast::{
self as ast, Constant, Expr, Operator, ParameterWithDefault, Parameters, Stmt,
};
@@ -319,7 +318,7 @@ pub fn is_type_checking_block(stmt: &ast::StmtIf, semantic: &SemanticModel) -> b
..
}) = test.as_ref()
{
if value.is_zero() {
if *value == 0 {
return true;
}
}