Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add fix for E261 #8114

Merged
merged 1 commit into from
Oct 22, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use ruff_diagnostics::Violation;
use ruff_diagnostics::{AlwaysFixableViolation, Diagnostic, Edit, Fix, Violation};
use ruff_macros::{derive_message_formats, violation};
use ruff_python_parser::TokenKind;
use ruff_python_trivia::PythonWhitespace;
Expand Down Expand Up @@ -30,11 +30,15 @@ use crate::rules::pycodestyle::rules::logical_lines::LogicalLine;
#[violation]
pub struct TooFewSpacesBeforeInlineComment;

impl Violation for TooFewSpacesBeforeInlineComment {
impl AlwaysFixableViolation for TooFewSpacesBeforeInlineComment {
#[derive_message_formats]
fn message(&self) -> String {
format!("Insert at least two spaces before an inline comment")
}

fn fix_title(&self) -> String {
format!("Insert spaces")
}
}

/// ## What it does
Expand Down Expand Up @@ -155,10 +159,15 @@ pub(crate) fn whitespace_before_comment(
let is_inline_comment = !line_text.trim_whitespace().is_empty();
if is_inline_comment {
if range.start() - prev_end < " ".text_len() {
context.push(
let mut diagnostic = Diagnostic::new(
TooFewSpacesBeforeInlineComment,
TextRange::new(prev_end, range.start()),
);
diagnostic.set_fix(Fix::safe_edit(Edit::range_replacement(
" ".to_string(),
TextRange::new(prev_end, range.start()),
)));
context.push_diagnostic(diagnostic);
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,22 @@
---
source: crates/ruff_linter/src/rules/pycodestyle/mod.rs
---
E26.py:2:5: E261 Insert at least two spaces before an inline comment
E26.py:2:5: E261 [*] Insert at least two spaces before an inline comment
|
1 | #: E261:1:5
2 | pass # an inline comment
| ^ E261
3 | #: E262:1:12
4 | x = x + 1 #Increment x
|
= help: Insert spaces

ℹ Fix
1 1 | #: E261:1:5
2 |-pass # an inline comment
2 |+pass # an inline comment
3 3 | #: E262:1:12
4 4 | x = x + 1 #Increment x
5 5 | #: E262:1:12


Loading