From f0dfce91e796ade2fbd670fe9a1e893a90f4b32a Mon Sep 17 00:00:00 2001 From: Charlie Marsh Date: Sat, 5 Nov 2022 19:47:06 -0400 Subject: [PATCH] Only track noqa directives for multi-line strings --- resources/test/fixtures/E501.py | 17 ++++++++++++++ src/noqa.rs | 5 ++++- .../ruff__linter__tests__E501_E501.py.snap | 22 +++++++++++++++++++ 3 files changed, 43 insertions(+), 1 deletion(-) diff --git a/resources/test/fixtures/E501.py b/resources/test/fixtures/E501.py index 34a7dca9c8f634..7fdacd71c3b764 100644 --- a/resources/test/fixtures/E501.py +++ b/resources/test/fixtures/E501.py @@ -32,3 +32,20 @@ def caller(string: str) -> None: Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. """, # noqa: E501 ) + +multiple_strings_per_line = { + # OK + "Lorem ipsum dolor": "sit amet", + # E501 Line too long + "Lorem ipsum dolor": "sit amet consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.", + # E501 Line too long + "Lorem ipsum dolor": """ +sit amet consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. +""", + # OK + "Lorem ipsum dolor": "sit amet consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.", # noqa: E501 + # OK + "Lorem ipsum dolor": """ +sit amet consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. +""", # noqa: E501 +} diff --git a/src/noqa.rs b/src/noqa.rs index ece95c240b41d0..c8b58d65f1c9fc 100644 --- a/src/noqa.rs +++ b/src/noqa.rs @@ -49,7 +49,10 @@ pub fn extract_noqa_line_for(lxr: &[LexResult]) -> Vec { if matches!(tok, Tok::EndOfFile) { break; } - if matches!(tok, Tok::String { .. }) { + // For multi-line strings, we expect `noqa` directives on the last line of the string. + // By definition, we can't have multiple multi-line strings on the same line, so we don't + // need to verify that we haven't already traversed past the current line. + if matches!(tok, Tok::String { .. }) && end.row() > start.row() { for i in (noqa_line_for.len())..(start.row() - 1) { noqa_line_for.push(i + 1); } diff --git a/src/snapshots/ruff__linter__tests__E501_E501.py.snap b/src/snapshots/ruff__linter__tests__E501_E501.py.snap index d75bee537e85f2..fc48cd87fc1660 100644 --- a/src/snapshots/ruff__linter__tests__E501_E501.py.snap +++ b/src/snapshots/ruff__linter__tests__E501_E501.py.snap @@ -24,4 +24,26 @@ expression: checks row: 25 column: 127 fix: ~ +- kind: + LineTooLong: + - 132 + - 88 + location: + row: 40 + column: 0 + end_location: + row: 40 + column: 132 + fix: ~ +- kind: + LineTooLong: + - 105 + - 88 + location: + row: 43 + column: 0 + end_location: + row: 43 + column: 105 + fix: ~