Skip to content

Commit

Permalink
Add a non-error test and its corresponding fix.
Browse files Browse the repository at this point in the history
  • Loading branch information
hoel-bagard committed Nov 12, 2023
1 parent 2283019 commit 06618c2
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 11 deletions.
12 changes: 12 additions & 0 deletions crates/ruff_linter/resources/test/fixtures/pycodestyle/E30.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,18 @@ def func2():
# end


# No error
class Class(object):

def func1():
pass

# comment
def func2():
pass
# end


# No error
class Class:

Expand Down
1 change: 1 addition & 0 deletions crates/ruff_linter/src/checkers/logical_lines.rs
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,7 @@ pub(crate) fn check_logical_lines(
&mut blank_lines_tracking_vars,
prev_indent_level,
indent_level,
indent_size,
locator,
stylist,
&mut context,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,8 @@ pub(crate) struct BlankLinesTrackingVars {
is_first_logical_line: bool,
/// This needs to be tracked between lines since the `is_in_class` and `is_in_fn` are set to
/// false when a comment is set dedented, but E305 should trigger on the next non-comment line.
follows_comment_after_class_or_fn: bool,
follows_comment_after_fn: bool,
follows_comment_after_class: bool,
}

impl Default for BlankLinesTrackingVars {
Expand All @@ -42,7 +43,8 @@ impl Default for BlankLinesTrackingVars {
is_in_fn: false,
fn_indent_level: 0,
is_first_logical_line: true,
follows_comment_after_class_or_fn: false,
follows_comment_after_fn: false,
follows_comment_after_class: false,
}
}
}
Expand Down Expand Up @@ -344,6 +346,7 @@ pub(crate) fn blank_lines(
tracked_vars: &mut BlankLinesTrackingVars,
prev_indent_level: Option<usize>,
indent_level: usize,
indent_size: usize,
locator: &Locator,
stylist: &Stylist,
context: &mut LogicalLinesContext,
Expand All @@ -366,27 +369,40 @@ pub(crate) fn blank_lines(
}

let mut follows_class_or_fn = false;
if indent_level <= tracked_vars.class_indent_level && tracked_vars.is_in_class {
if indent_level < tracked_vars.class_indent_level && tracked_vars.is_in_class {
tracked_vars.is_in_class = false;
if line.is_comment_only() {
tracked_vars.follows_comment_after_class_or_fn = true
tracked_vars.follows_comment_after_class = true
} else {
follows_class_or_fn = true;
}
}

if indent_level <= tracked_vars.fn_indent_level && tracked_vars.is_in_fn {
if indent_level < tracked_vars.fn_indent_level && tracked_vars.is_in_fn {
tracked_vars.is_in_fn = false;
if line.is_comment_only() {
tracked_vars.follows_comment_after_class_or_fn = true
tracked_vars.follows_comment_after_fn = true
} else {
follows_class_or_fn = true;
}
}

if tracked_vars.follows_comment_after_class_or_fn && !line.is_comment_only() {
follows_class_or_fn = true;
tracked_vars.follows_comment_after_class_or_fn = false;
if tracked_vars.follows_comment_after_fn && !line.is_comment_only() {
if indent_level == tracked_vars.fn_indent_level {
tracked_vars.is_in_fn = true;
} else {
follows_class_or_fn = true;
}
tracked_vars.follows_comment_after_fn = false;
}

if tracked_vars.follows_comment_after_class && !line.is_comment_only() {
if indent_level == tracked_vars.class_indent_level {
tracked_vars.is_in_class = true;
} else {
follows_class_or_fn = true;
}
tracked_vars.follows_comment_after_class = false;
}

for token in line.tokens().iter() {
Expand Down Expand Up @@ -537,7 +553,7 @@ pub(crate) fn blank_lines(
match token.kind() {
TokenKind::Class => {
if !tracked_vars.is_in_class {
tracked_vars.class_indent_level = indent_level;
tracked_vars.class_indent_level = indent_level + indent_size;
}
tracked_vars.is_in_class = true;
tracked_vars.follows_decorator = false;
Expand All @@ -551,7 +567,7 @@ pub(crate) fn blank_lines(
}
TokenKind::Def => {
if !tracked_vars.is_in_fn {
tracked_vars.fn_indent_level = indent_level;
tracked_vars.fn_indent_level = indent_level + indent_size;
}
tracked_vars.is_in_fn = true;
tracked_vars.follows_def = true;
Expand Down

0 comments on commit 06618c2

Please sign in to comment.