Commit Graph
58 Commits
Author SHA1 Message Date
astral-automations-bot[bot]andBrent Westbrook a7ba68b491 Bump version to 0.17.6 (#30578)
Co-authored-by: astral-automations-bot[bot] <305554984+astral-automations-bot[bot]@users.noreply.github.com>
Co-authored-by: Brent Westbrook <36778786+ntBre@users.noreply.github.com>
2026-11-19 10:32:47 -05:00
0ebb77d35c Bump version to 0.17.5 (#30380)
Co-authored-by: astral-automations-bot[bot] <305554984+astral-automations-bot[bot]@users.noreply.github.com>
Co-authored-by: Brent Westbrook <36778786+ntBre@users.noreply.github.com>
Co-authored-by: Brent Westbrook <brentrwestbrook@gmail.com>
2026-11-12 17:26:23 +00:00
astral-automations-bot[bot]andBrent Westbrook 05cdc36a45 Bump version to 0.17.4 (#30073)
Co-authored-by: astral-automations-bot[bot] <305554984+astral-automations-bot[bot]@users.noreply.github.com>
Co-authored-by: Brent Westbrook <36778786+ntBre@users.noreply.github.com>
2026-11-05 13:22:15 -05:00
astral-automations-bot[bot]andBrent Westbrook ba65abf8ba Bump version to 0.17.3 (#29745)
Co-authored-by: astral-automations-bot[bot] <305554984+astral-automations-bot[bot]@users.noreply.github.com>
Co-authored-by: Brent Westbrook <36778786+ntBre@users.noreply.github.com>
2026-10-26 20:14:27 -04:00
Anish Giri 601192dfdb [pyupgrade] Avoid incorrect conversions to f-strings (UP032) (#25485)
## Summary

Fixes part of #15874: UP032 emitted broken fixes for str.format calls
with no f-string equivalent. Non-identifier attributes, quote-clashing
or backslash keys, unknown conversions, and mixed numbering are now
skipped. Signed indices like {+0} were a parser bug (leading +
accepted), which also affected F523/F524

## Test Plan

New mdtest at crates/ruff_linter/resources/mdtest/pyupgrade/f-string.md
plus parser unit tests
2026-10-23 16:24:46 -04:00
astral-automations-bot[bot]andBrent Westbrook 30516b42ab Bump version to 0.17.2 (#29637)
Co-authored-by: astral-automations-bot[bot] <305554984+astral-automations-bot[bot]@users.noreply.github.com>
Co-authored-by: Brent Westbrook <36778786+ntBre@users.noreply.github.com>
2026-10-22 13:54:24 -04:00
astral-automations-bot[bot] 4683270c69 Bump version to 0.17.1 (#29420)
Co-authored-by: astral-automations-bot[bot] <305554984+astral-automations-bot[bot]@users.noreply.github.com>
2026-10-15 10:56:41 -04:00
Alex Waygood a7b52a1565 [ty] Preserve distinct string literal displays without deprecated ICU data (#29206)
Canonical equivalence can make distinct string values look the same. ty
escapes their displays to distinguish them, but the current approach
relies on ICU4X's deprecated `NfcInert` property, which [ICU4X plans to
remove](https://github.com/unicode-org/icu4x/issues/7892), and can
escape characters that do not need escaping.

Replace inertness checks with NFC checks on runs of unescaped text. When
adding a character would make a run non-NFC, escape that character and
continue checking after it. The resulting string literal is in NFC and
evaluates to the original value, so canonically equivalent values remain
distinguishable.

For example, `\u212b` (the Angstrom sign) is canonically equivalent to
`Å`. ty now keeps the unrelated `é` visible while still distinguishing
the two values:

```py
reveal_type("éÅ")
# main:    Literal["éÅ"]
# this PR: Literal["éÅ"]

reveal_type("é\u212b")
# main:    Literal["\xe9\u212b"]
# this PR: Literal["é\u212b"]
```

The change also uses Unicode grapheme-break properties to keep
characters visually separate from the quotes and escape sequences around
them. It escapes the braille blank because it can look like an ordinary
space.
2026-10-13 15:04:47 +00:00
astral-automations-bot[bot]andBrent Westbrook 5b27c1ccfa Bump version to 0.17.0 (#29259)
Co-authored-by: astral-automations-bot[bot] <305554984+astral-automations-bot[bot]@users.noreply.github.com>
Co-authored-by: Brent Westbrook <brentrwestbrook@gmail.com>
2026-10-09 13:45:29 -04:00
renovate[bot]andBrent Westbrook fbbe89dc71 Update Rust crate icu_normalizer to v2.3.0 (#29166)
This PR contains the following updates:

| Package | Type | Update | Change |
|---|---|---|---|
| [icu_normalizer](https://icu4x.unicode.org)
([source](https://redirect.github.com/unicode-org/icu4x)) |
workspace.dependencies | minor | `2.2.0` → `2.3.0` |

---

### Configuration

📅 **Schedule**: (UTC)

- Branch creation
  - "before 4am on Wednesday"
- Automerge
  - At any time (no schedule defined)

🚦 **Automerge**: Disabled by config. Please merge this manually once you
are satisfied.

♻ **Rebasing**: Whenever PR becomes conflicted, or you tick the
rebase/retry checkbox.

🔕 **Ignore**: Close this PR and you won't be reminded about this update
again.

---

- [ ] <!-- rebase-check -->If you want to rebase/retry this PR, check
this box

---

This PR was generated by [Mend Renovate](https://mend.io/renovate/).
View the [repository job
log](https://developer.mend.io/github/astral-sh/ruff).

<!--renovate-debug:eyJjcmVhdGVkSW5WZXIiOiI0NC4xMzQuMSIsInVwZGF0ZWRJblZlciI6IjQ0LjEzNC4xIiwidGFyZ2V0QnJhbmNoIjoibWFpbiIsImxhYmVscyI6WyJpbnRlcm5hbCJdfQ==-->

---------

Co-authored-by: renovate[bot] <29139614+renovate[bot]@users.noreply.github.com>
Co-authored-by: Brent Westbrook <brentrwestbrook@gmail.com>
2026-10-07 16:40:18 -04:00
Alex Waygood 57a6e86342 [ty] Improve display of string literal types that include unicode characters or double quotes (#29089)
ty currently displays some string-literal types with Rust-style escapes
that are invalid in Python. For example, a string containing U+200B is
shown as `Literal["\u{200b}"]`; Python uses `\u200b` for this character.
ty also always uses double quotes, so a value such as `'a"b'` is shown
as `Literal["a\"b"]`.

This PR uses Python-style escapes and prefers double quotes, switching
to single quotes when the value contains double quotes but no single
quotes.

It escapes default-ignorable characters, which can be invisible, and
combining marks that would otherwise attach to an opening quote or
escape sequence. For example, `"\u0301"` is displayed as
`Literal["\u0301"]`, preventing the mark from attaching to the opening
quote. It also escapes certain characters in parts of a string that are
not in [Unicode Normalization Form C (NFC)][NFC], so visually identical
values such as `"é"` and `"e\u0301"` are displayed distinctly as
`Literal["é"]` and `Literal["e\u0301"]`.

The following table compares the displayed types for the string values
`'a"b'`, `"\u200b"` (a zero-width space), `"é"`, `"e\u0301"`, and
`"\u0301"`, targeting Python 3.12:

The `"\u200b"` and `"\u0301"` columns use screenshots because some
checkers display an invisible zero-width space or leave a standalone
combining mark unescaped, which can render differently across browsers.

| Checker | `'a"b'` | `"\u200b"` | `"é"` | `"e\u0301"` | `"\u0301"` |
|---|---|---|---|---|---|
| ty on `main` | `Literal["a\"b"]` | <img width="194" height="30"
alt="Literal[&quot;\u{200b}&quot;]"
src="https://github.com/user-attachments/assets/0199b02e-5049-425e-8b37-5c0fc602c0c9"
/> | `Literal["é"]` | `Literal["e\u{301}"]` | <img width="184"
height="30" alt="Literal[&quot;\u{301}&quot;]"
src="https://github.com/user-attachments/assets/ce711f02-eda4-469a-8c86-51adf2665b99"
/> |
| ty with this PR | `Literal['a"b']` | <img width="174" height="30"
alt="Literal[&quot;\u200b&quot;]"
src="https://github.com/user-attachments/assets/777ac4df-418f-467b-b3dd-141e45bd1ea4"
/> | `Literal["é"]` | `Literal["e\u0301"]` | <img width="174"
height="30" alt="Literal[&quot;\u0301&quot;]"
src="https://github.com/user-attachments/assets/bb40d8e5-8257-487c-926c-709bc0ca3063"
/> |
| mypy 2.4.0 | `Literal['a"b']` | <img width="174" height="30"
alt="Literal['\u200b']"
src="https://github.com/user-attachments/assets/fa80c519-aa27-44a8-b622-2ee6aa6b2f04"
/> | `Literal['é']` | `Literal['é']` | <img width="116" height="30"
alt="Literal['&lt;unescaped combining acute accent&gt;']"
src="https://github.com/user-attachments/assets/863a132d-ba19-4bff-a8aa-623fb59d548b"
/> |
| Pyrefly 1.3.2 | `Literal['a"b']` | <img width="116" height="30"
alt="Literal['&lt;unescaped zero-width space&gt;']"
src="https://github.com/user-attachments/assets/fec1f052-06ad-4e20-b973-403c8b67eb48"
/> | `Literal['é']` | `Literal['é']` | <img width="116" height="30"
alt="Literal['&lt;unescaped combining acute accent&gt;']"
src="https://github.com/user-attachments/assets/9981d961-47e3-478d-8750-be68c4415242"
/> |
| Pyright 1.1.414 | `Literal['a"b']` | <img width="116" height="30"
alt="Literal['&lt;unescaped zero-width space&gt;']"
src="https://github.com/user-attachments/assets/5dd25b1e-72cb-4975-ad00-9b1fad3d4a28"
/> | `Literal['é']` | `Literal['é']` | <img width="116" height="30"
alt="Literal['&lt;unescaped combining acute accent&gt;']"
src="https://github.com/user-attachments/assets/d9d6148f-8135-485e-9767-7a5f967a9faa"
/> |
| Zuban 0.10.0 | `Literal['a"b']` | <img width="116" height="30"
alt="Literal['&lt;unescaped zero-width space&gt;']"
src="https://github.com/user-attachments/assets/9bb47bf3-e148-415a-828e-ce84887cffaa"
/> | `Literal['é']` | `Literal['é']` | <img width="116" height="30"
alt="Literal['&lt;unescaped combining acute accent&gt;']"
src="https://github.com/user-attachments/assets/4edfdb70-4c1f-4e5e-b227-0ed039751c2a"
/> |
| Pycroscope 0.5.0 | `Literal['a"b']` | <img width="174" height="30"
alt="Literal['\u200b']"
src="https://github.com/user-attachments/assets/b3af3d85-64e5-4172-8265-5109b9055bd1"
/> | `Literal['é']` | `Literal['é']` | <img width="116" height="30"
alt="Literal['&lt;unescaped combining acute accent&gt;']"
src="https://github.com/user-attachments/assets/98efdbe1-753c-42d6-bd00-bfb839711eb9"
/> |

This rendering will also be used by the proposed [`non-exhaustive-match`
rule](https://github.com/astral-sh/ruff/pull/29053), whose diagnostics
show uncovered values and their types.

[NFC]: https://www.unicode.org/reports/tr15/#Norm_Forms
2026-10-06 10:28:46 +01:00
astral-automations-bot[bot]andBrent Westbrook 3265ed1f94 Bump version to 0.16.10 (#29055)
Co-authored-by: astral-automations-bot[bot] <305554984+astral-automations-bot[bot]@users.noreply.github.com>
Co-authored-by: Brent Westbrook <brentrwestbrook@gmail.com>
2026-10-01 13:40:09 -04:00
astral-automations-bot[bot]andBrent Westbrook 0be08a206f Bump version to 0.16.9 (#28882)
Co-authored-by: astral-automations-bot[bot] <305554984+astral-automations-bot[bot]@users.noreply.github.com>
Co-authored-by: Brent Westbrook <36778786+ntBre@users.noreply.github.com>
2026-09-24 16:21:50 -04:00
astral-automations-bot[bot]andBrent Westbrook 62914c4b9b Bump version to 0.16.8 (#28648)
Co-authored-by: astral-automations-bot[bot] <305554984+astral-automations-bot[bot]@users.noreply.github.com>
Co-authored-by: Brent Westbrook <36778786+ntBre@users.noreply.github.com>
2026-09-16 11:31:02 -04:00
astral-automations-bot[bot]andBrent Westbrook b5dba861cc Bump version to 0.16.7 (#28496)
Co-authored-by: astral-automations-bot[bot] <305554984+astral-automations-bot[bot]@users.noreply.github.com>
Co-authored-by: Brent Westbrook <36778786+ntBre@users.noreply.github.com>
2026-09-10 13:47:30 -04:00
Brent Westbrook 22f65a2ab5 Bump 0.16.6 (#28280) 2026-09-03 12:36:42 -04:00
Brent Westbrook 9e4938c4a6 Bump 0.16.5 (#28110) 2026-08-27 12:09:07 -04:00
Brent Westbrook 11c76bf48f Bump 0.16.4 (#27937) 2026-08-20 13:14:29 -04:00
Brent Westbrook b0e47022cf Bump 0.16.3 (#27723) 2026-08-13 09:39:16 -04:00
Andrej730 d23b7e880e [pylint] Fix false positives and negatives with %b format character (PLE1300, PLE1307) (#27560)
## Summary

Noticed case below with possible false positive/negative when handling
`%b` format character by rules
https://docs.astral.sh/ruff/rules/bad-string-format-character/ and
https://docs.astral.sh/ruff/rules/bad-string-format-type/

```python
# False negative: not flagged by bad-string-format-character
# Runtime: ValueError: unsupported format character 'b' (0x62) at index 7
a = "hello %b" % 25

# False positive: bad-string-format-type
# Runtime: ValueError: unsupported format character 'b' (0x62) at index 7
# It should report nothing, since the problem is not mismatching
# formatter and provided value, but use of invalid format character in general.
a = "hello %b" % "23"
```

This fix resolves both issues - first case is now reported and second
results in no diagnostic.

The root cause for was `bad-string-format-character` issue was
`CFormatString` parser always parsing `%b` as `Bytes` type, while for
bytes literals it's actually just an invalid character. Added
`CFormatContext` enum, so parser can parse `%b` only when invoked from
`CFormatBytes` and not from `CFormatString` and report issue when finds
`%b` in string literals formatters.

Second issue was caused by `bad-string-format-type` assuming `%b` is
allowed only for integers, so it was reporting any other type as a
mismatch. Now it just ignores `%b`, since it will be handled by
`bad-string-format-character`.


https://github.com/astral-sh/ruff/blob/17a00de2e298612201a8fe30790e9399204af1b9/crates/ruff_linter/src/rules/pylint/rules/bad_string_format_type.rs#L100

For completeness I also refactored `FormatType::from` in that rule to
rely on `CFormatType` variants instead of hardcoding formatter
characters It really just made it more clean without any functional
changes:
- `Ascii` is now handled explicitly as `Repr`, instead of assuming it's
unknown
- dropped non-existing formatters `n` and `%`, they are present from the
original implementation https://github.com/astral-sh/ruff/pull/2572 and
are not valid formatters (possibly `%` is artifact from old cformat
parser quirks).


## Test Plan

Added tests for both rules, updated snapshots. All previous tests pass
too.
2026-08-10 17:02:38 -04:00
Brent Westbrook 5b48a04097 Bump 0.16.2 (#27555) 2026-08-07 09:12:50 -04:00
Charlie Marsh 6cfeb8aa07 Narrow internal visibility with Hawk (#27339)
## Summary

This PR uses Hawk 0.1.11 to narrow internal declarations to the
visibility they actually need.

Here, I've intentionally _only_ made visibility changes; unused API
removals are split into follow-up PRs. In total, 1,201 declaration
visibility modifiers are narrowed. Public generated formatter APIs and
other declarations whose narrower visibility would activate dead-code or
API-shape lints remain unchanged.
2026-07-31 22:08:13 -04:00
Brent Westbrook 80790b348b Bump 0.16.1 (#27330) 2026-07-30 13:54:42 -04:00
Brent Westbrook a2635fd8f3 Bump 0.16.0 (#27136) 2026-07-23 14:37:34 -04:00
Alex Waygood 8ee2d3cca3 Propagate invalid format type characters from FormatSpec::parse() in ruff_python_literal (#26976) 2026-07-20 11:45:55 +01:00
Brent Westbrook 0177a7e0d2 Bump 0.15.22 (#26884) 2026-07-16 10:54:43 -04:00
Brent Westbrook 3e1f636451 Bump 0.15.21 (#26676) 2026-07-09 15:42:27 -04:00
Brent Westbrook f82a36b6ba Bump 0.15.20 (#26376) 2026-06-25 10:00:55 -07:00
Charlie Marsh 7f0436561d Bump version to 0.15.19 (#26291) 2026-06-23 20:42:26 -04:00
Charlie Marsh dff016b7a4 Publish Ruff crates to crates.io (#26271)
## Summary

Publish Ruff's production Rust crates to crates.io as part of the
existing release workflow. This follows uv's setup: workspace
dependencies carry registry-compatible version pins, internal crates
receive generated READMEs and an explicit versioning policy, CI dry-runs
the full workspace publish, and release jobs publish missing crate
versions idempotently through crates.io trusted publishing.

The publishing set contains 32 packages: Ruff's CLI, WASM package,
server, linter, formatter, parser, and the internal Ruff component
crates required by them. It also includes `ty_module_resolver`,
`ty_site_packages`, and `ty_static`, because those three crates are part
of Ruff's normal dependency closure.

The publishing set deliberately excludes:

- Test-only crates: `mdtest`, `ruff_mdtest`,
`ruff_python_ast_integration_tests`, and
`ruff_python_trivia_integration_tests`.
- Benchmark and development crates: `ruff_benchmark` and `ruff_dev`.
- All remaining `ty_*` crates.

These packages retain `publish = false`, so bootstrap, version bumping,
README generation, CI dry-runs, and release publishing skip them through
Cargo metadata instead of a separate hard-coded allowlist.

The crates.io job is intentionally independent of release announcement,
so a crates.io outage cannot block the existing binary, PyPI, npm, or
GitHub release outputs. A bootstrap script reserves new crate names,
configures the release workflow as each crate's trusted publisher,
enables trusted-publishing-only releases, and records configured crates
for idempotent reruns.

The full workspace publish dry-run packages and verifies all 32 selected
crates.

Closes https://github.com/astral-sh/ruff/issues/43.
2026-06-23 12:49:23 -04:00
Shahar NavehandMicha Reiser b03dc7841c Replace unmaintained unic-ucd-category crate with icu_properties (#24344)
Co-authored-by: Micha Reiser <micha@reiser.io>
2026-04-02 12:29:22 +00:00
William Collishaw 445a59d294 replace deprecated std::f64::EPSILON with f64::EPSILON (#24067) 2026-03-20 08:41:37 +01:00
Denys ZhakandBrent Westbrook 1d94f26cad [pyupgrade] Fix handling of \N in raw strings (UP032) (#22149)
**Summary:**

Fixes UP032 autofix incorrectly converting raw strings with `\N{...}` to
f-strings, which changes semantics and causes runtime errors.

Fixes #22060 

## Test Plan

- Added test case for raw strings with \N{...}
- Regular strings with \N{...} still autofix correctly
- All 119 pyupgrade tests pass

---------

Co-authored-by: Brent Westbrook <brentrwestbrook@gmail.com>
2026-02-20 15:12:21 +00:00
Phong Do b0bc990cbf [pyupgrade] Fix parsing named Unicode escape sequences (UP032) (#21901)
## Summary

Fixes https://github.com/astral-sh/ruff/issues/19771

Fixes incorrect parsing of Unicode named escape sequences like `Hey
\N{snowman}` in `FormatString`, which were being incorrectly split into
separate literal and field parts instead of being treated as a single
literal unit.

## Problem

The `FormatString` parser incorrectly handles Unicode named escape
sequences:
- **Current**: `Hey \N{snowman}` is parsed into 2 parts `Literal("Hey
\N")` & `Field("snowman")`
- **Expected**: `Hey \N{snowman}` should be parsed into 1 part
`Literal("Hey \N{snowman}")`

This affects f-string conversion rules when fixing `UP032` that rely on
proper format string parsing.

## Solution

I modified `parse_literal` to detect and handle Unicode named escape
sequences before parsing single characters:
- Introduced a flag to track when a backslash is "available" to escape
something.
- When the flag is `true`, and the text starts with `N{`, try to parse
the complete Unicode escape sequence as one unit, and set the flag to
`false` after parsing successfully.
- Set the flag to `false` when the backslash is already consumed.

## Manual Verification

`"\N{angle}AOB = {angle}°".format(angle=180)` 

**Result**

```bash
 def foo():
-    "\N{angle}AOB = {angle}°".format(angle=180)
+    f"\N{angle}AOB = {180}°"

Would fix 1 error.
```

`"\N{snowman} {snowman}".format(snowman=1)`

**Result**
```bash
 def foo():
-    "\N{snowman} {snowman}".format(snowman=1)
+    f"\N{snowman} {1}"

Would fix 1 error.
```

`"\\N{snowman} {snowman}".format(snowman=1)`

**Result**
```bash
 def foo():
-    "\\N{snowman} {snowman}".format(snowman=1)
+    f"\\N{1} {1}"

Would fix 1 error.
```

## Test Plan

- Added test cases (happy case, invalid case, edge case) for
`FormatString` when parsing Unicode escape sequence.
- Updated snapshots.
2025-12-16 16:33:39 -05:00
Micha Reiser 9ae698fe30 Switch to Rust 2024 edition (#18129) 2025-05-16 13:25:28 +02:00
Micha Reiser fa628018b2 Use #[expect(lint)] over #[allow(lint)] where possible (#17822) 2025-05-03 21:20:31 +02:00
Brent WestbrookandAlex Waygood b5e5271adf Preserve triple quotes and prefixes for strings (#15818)
## Summary

This is a follow-up to #15726, #15778, and #15794 to preserve the triple
quote and prefix flags in plain strings, bytestrings, and f-strings.

I also added a `StringLiteralFlags::without_triple_quotes` method to
avoid passing along triple quotes in rules like SIM905 where it might
not make sense, as discussed
[here](https://github.com/astral-sh/ruff/pull/15726#discussion_r1930532426).

## Test Plan

Existing tests, plus many new cases in the `generator::tests::quote`
test that should cover all combinations of quotes and prefixes, at least
for simple string bodies.

Closes #7799 when combined with #15694, #15726, #15778, and #15794.

---------

Co-authored-by: Alex Waygood <Alex.Waygood@Gmail.com>
2025-02-04 08:41:06 -05:00
Micha Reiser b63c2e126b Upgrade Rust toolchain to 1.83 (#14677) 2024-11-29 12:05:05 +00:00
Simon Brugman 84d4f114ef Use bitshift consistently for bitflag definitions (#14265) 2024-11-11 10:20:17 +00:00
Charlie Marsh 16acd4913f Remove some unused pub functions (#11576)
## Summary

I left anything in `red-knot`, any `with_` methods, etc.
2024-05-28 09:56:51 -04:00
Charlie Marsh af60d539ab Move sub-crates to workspace dependencies (#11407)
## Summary

This matches the setup we use in `uv` and allows for consistency in the
`Cargo.toml` files.
2024-05-13 14:37:50 +00:00
Micha Reiser 17d56ccab3 Remove unused dependencies (#10475)
## Summary
I used `cargo-shear` (see
[tweet](https://twitter.com/boshen_c/status/1770106165923586395)) to
remove some unused dependencies that `cargo udeps` wasn't reporting.

<!-- What's the purpose of the change? What does it do, and why? -->

## Test Plan

`cargo test`
2024-03-19 17:33:47 +01:00
Alex Waygood c2e15f38ee Unify enums used for internal representation of quoting style (#10383) 2024-03-13 17:19:17 +00:00
Micha Reiser 341c2698a7 Run doctests as part of CI pipeline (#9939) 2024-02-12 10:18:58 +01:00
Micha Reiser 94968fedd5 Use Rust 1.75 toolchain (#9437) 2024-01-08 18:03:16 +01:00
Charlie Marsh 9073220887 Make all dependencies workspace dependencies (#9333)
## Summary

This PR modifies our `Cargo.toml` files to use workspace dependencies
for _all_ dependencies, rather than the status quo of sporadically
trying to use workspace dependencies for those dependencies that are
used across multiple crates. I find the current situation more confusing
and harder to manage, since we have a mix of workspace and crate-local
dependencies, whereas this setup consistently uses the same approach for
all dependencies.
2024-01-02 13:41:59 +00:00
Andrew Gallant f585e3e2dc remove several uses of unsafe (#8600)
This PR removes several uses of `unsafe`. I generally limited myself to
low hanging fruit that I could see. There are still a few remaining uses
of `unsafe` that looked a bit more difficult to remove (if possible at
all). But this gets rid of a good chunk of them.

I put each `unsafe` removal into its own commit with a justification for
why I did it. So I would encourage reviewing this PR commit-by-commit.
That way, we can legislate them independently. It's no problem to drop a
commit if we feel the `unsafe` should stay in that case.
2023-11-28 09:50:03 -05:00
konstiandCharlie Marsh 14e65afdc6 Update to Rust 1.74 and use new clippy lints table (#8722)
Update to [Rust
1.74](https://blog.rust-lang.org/2023/11/16/Rust-1.74.0.html) and use
the new clippy lints table.

The update itself introduced a new clippy lint about superfluous hashes
in raw strings, which got removed.

I moved our lint config from `rustflags` to the newly stabilized
[workspace.lints](https://doc.rust-lang.org/stable/cargo/reference/workspaces.html#the-lints-table).
One consequence is that we have to `unsafe_code = "warn"` instead of
"forbid" because the latter now actually bans unsafe code:

```
error[E0453]: allow(unsafe_code) incompatible with previous forbid
  --> crates/ruff_source_file/src/newlines.rs:62:17
   |
62 |         #[allow(unsafe_code)]
   |                 ^^^^^^^^^^^ overruled by previous forbid
   |
   = note: `forbid` lint level was set on command line
```

---------

Co-authored-by: Charlie Marsh <charlie.r.marsh@gmail.com>
2023-11-16 18:12:46 -05:00
Charlie Marsh 93b5d8a0fb Implement our own small-integer optimization (#7584)
## Summary

This is a follow-up to #7469 that attempts to achieve similar gains, but
without introducing malachite. Instead, this PR removes the `BigInt`
type altogether, instead opting for a simple enum that allows us to
store small integers directly and only allocate for values greater than
`i64`:

```rust
/// A Python integer literal. Represents both small (fits in an `i64`) and large integers.
#[derive(Clone, PartialEq, Eq, Hash)]
pub struct Int(Number);

#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub enum Number {
    /// A "small" number that can be represented as an `i64`.
    Small(i64),
    /// A "large" number that cannot be represented as an `i64`.
    Big(Box<str>),
}

impl std::fmt::Display for Number {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            Number::Small(value) => write!(f, "{value}"),
            Number::Big(value) => write!(f, "{value}"),
        }
    }
}
```

We typically don't care about numbers greater than `isize` -- our only
uses are comparisons against small constants (like `1`, `2`, `3`, etc.),
so there's no real loss of information, except in one or two rules where
we're now a little more conservative (with the worst-case being that we
don't flag, e.g., an `itertools.pairwise` that uses an extremely large
value for the slice start constant). For simplicity, a few diagnostics
now show a dedicated message when they see integers that are out of the
supported range (e.g., `outdated-version-block`).

An additional benefit here is that we get to remove a few dependencies,
especially `num-bigint`.

## Test Plan

`cargo test`
2023-09-25 15:13:21 +00:00
Zanie Blue 100904adb9 Avoid parsing other parts of a format specification if replacements are present (#6858)
Closes #6767
Replaces https://github.com/astral-sh/ruff/pull/6773 (this cherry-picks
some parts from there)
Alternative to the approach introduced in #6616 which added support for
placeholders in format specifications while retaining parsing of other
format specification parts.

The idea is that if there are placeholders in a format specification we
will not attempt to glean semantic meaning from the other parts of the
format specification we'll just extract all of the placeholders ignoring
other characters. The dynamic content of placeholders can drastically
change the meaning of the format specification in ways unknowable by
static analysis. This change prevents false analysis and will ensure
safety if we build other rules on top of this at the cost of missing
detection of some bad specifications.

Minor note: I've use "replacements" and "placeholders" interchangeably
but am trying to go with "placeholder" as I think it's a better term for
the static analysis concept here
2023-08-25 17:42:57 +00:00