Skip to content

Autofix S608 when fstring has no placeholders #17967

@dosisod

Description

@dosisod

Summary

I ran into this on one of my codebases today:

from sqlite3 import connect

db = connect(":memory:")

db.execute(f"SELECT * FROM table").fetchall()

Running with ruff v0.11.8:

$ ruff check x.py --select S608,F541
x.py:5:12: F541 [*] f-string without any placeholders
  |
3 | db = connect(":memory:")
4 |
5 | db.execute(f"SELECT * FROM table").fetchall()
  |            ^^^^^^^^^^^^^^^^^^^^^^ F541
  |
  = help: Remove extraneous `f` prefix

x.py:5:12: S608 Possible SQL injection vector through string-based query construction
  |
3 | db = connect(":memory:")
4 |
5 | db.execute(f"SELECT * FROM table").fetchall()
  |            ^^^^^^^^^^^^^^^^^^^^^^ S608
  |

Found 2 errors.
[*] 1 fixable with the `--fix` option.

In this case, fixing F541 by removing the f will also fix S608, which means you can do one of the following:

  • Emit both errors, and mark them both as fixable
  • Emit just S608, and mark it as fixable (by applying the fix logic from S541)

It might be more effort than it's worth to improve this, but it would be an easy case to detect in S608.

Version

ruff 0.11.8

Metadata

Metadata

Assignees

No one assigned

    Labels

    help wantedContributions especially welcomeruleImplementing or modifying a lint rule

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions