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

Make it possible to correctly indent snippet_block snippets #5134

Merged
merged 11 commits into from
Feb 6, 2020

Conversation

flip1995
Copy link
Member

@flip1995 flip1995 commented Feb 4, 2020

This adds a indent_relative_to arg to the {snippet,expr}_block functions. This makes it possible to keep the correct indentation of block like suggestions.

In addition, this makes the trim_multiline function private and adds a indent_of function, to get the indentation of the first line of a span.

The suggestion of needless_continue cannot be made auto applicable, since it would be also necessary to remove code following the linted expression. (Well, maybe it is possible, but I don't know how to do it. Expanding the suggestion span to the last expression, that should be removed didn't work)

changelog: Improve suggestions, when blocks of code are involved

@flip1995 flip1995 added the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties label Feb 4, 2020
@@ -261,7 +261,7 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for Attributes {
_ => {},
}
}
let line_span = last_line_of_span(cx, attr.span);
let line_span = first_line_of_span(cx, attr.span);
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This function never returned the last line of the span, but always the first line, so I renamed it accordingly.

Copy link
Member

@phansch phansch left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM overall, nice work!

if (zero!(i % 2) || nonzero!(i % 5)) && i % 3 != 0 {
continue;
} {
println!("Blabber");
Copy link
Member

@phansch phansch Feb 5, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This seems to be a regression as it now wraps the removed else part in a block?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, it changed a little bit, but I would keep it that way for two reasons:

  1. It's too hard to align this correctly after removing the block
  2. Suggesting it like this prevents a bug, that could be produced by the suggestion:
let y: u32 = 23;
if x {
    continue;
} else {
    let y: u32 = 42;
    // ...
}

println!("{}", y); // outputs 23

previous, the suggestion would change the output:

let y: u32 = 23; // best case: a warning for unused var is produced
if x {
    continue;
}
let y: u32 = 42;
// ...

println!("{}", y); // outputs 42

now the output stays the same:

let y: u32 = 23;
if x {
    continue;
} 
{
    let y: u32 = 42;
    // ...
}

println!("{}", y); //outputs 23

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@bors
Copy link
Collaborator

bors commented Feb 6, 2020

☔ The latest upstream changes (presumably #5125) made this pull request unmergeable. Please resolve the merge conflicts.

@phansch
Copy link
Member

phansch commented Feb 6, 2020

r=me with rebase

@flip1995
Copy link
Member Author

flip1995 commented Feb 6, 2020

@bors r=phansch

@bors
Copy link
Collaborator

bors commented Feb 6, 2020

📌 Commit e23881e has been approved by phansch

@bors
Copy link
Collaborator

bors commented Feb 6, 2020

⌛ Testing commit e23881e with merge b5e6d6d...

bors added a commit that referenced this pull request Feb 6, 2020
Make it possible to correctly indent snippet_block snippets

This adds a `indent_relative_to` arg to the `{snippet,expr}_block` functions. This makes it possible to keep the correct indentation of block like suggestions.

In addition, this makes the `trim_multiline` function private and adds a `indent_of` function, to get the indentation of the first line of a span.

The suggestion of `needless_continue` cannot be made auto applicable, since it would be also necessary to remove code following the linted expression. (Well, maybe it is possible, but I don't know how to do it. Expanding the suggestion span to the last expression, that should be removed didn't work)

changelog: Improve suggestions, when blocks of code are involved
@bors
Copy link
Collaborator

bors commented Feb 6, 2020

☀️ Test successful - checks-travis, status-appveyor
Approved by: phansch
Pushing b5e6d6d to master...

@bors bors merged commit e23881e into rust-lang:master Feb 6, 2020
@flip1995 flip1995 deleted the snippet_block branch May 4, 2020 15:30
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
S-waiting-on-review Status: Awaiting review from the assignee but also interested parties
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants