Skip to content

Autofix overwrites wrong line with multiple multiline matches #3388

@JuliusDegesys

Description

@JuliusDegesys

Describe the bug
Incorrect chunks of the file are auto-fixed. Searching for assert_eq(True, $X) and replacing with assert $X will mangle the output when auto-fixing multiple multi-line matches.

To Reproduce
https://semgrep.dev/s/juliusdegesys:asserteqbug
Input file:

assert_eq(
    True, "a"
)
assert_eq(
    True, "b"
)
x = "abc"

Rule:

rules:
    - id: assert_eq-true
      message: Change assert_eq(True, x) to assert x
      severity: INFO
      languages:
      - python
      pattern: assert_eq(True, $ACTUAL)
      fix: assert $ACTUAL

The matches look correct, and so do the auto-fixes. But when you "Apply all fixes" or apply the first fix followed by the second, the following output is generated:

assert "a"
assert_eq(
    True, "b"
assert "b"

Expected behavior

assert "a"
assert "b"
x = "abc"

What is the priority of the bug to you?

  • P0: blocking your adoption of Semgrep or workflow
  • P1: important to fix or quite annoying
  • P2: regular bug that should get fixed

Environment
If not using semgrep.dev: are you running off docker, an official binary, a local build?

pip install semgrep
$ semgrep --version
0.56.0

Metadata

Metadata

Assignees

No one assigned

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions