Skip to content

Commit

Permalink
fix empty after lint on impl/trait items
Browse files Browse the repository at this point in the history
  • Loading branch information
jdonszelmann authored and GuillaumeGomez committed Feb 3, 2025
1 parent af2aa59 commit 7783dcd
Show file tree
Hide file tree
Showing 5 changed files with 121 additions and 34 deletions.
99 changes: 66 additions & 33 deletions clippy_lints/src/empty_line_after.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,13 @@ use clippy_utils::source::{SpanRangeExt, snippet_indent};
use clippy_utils::tokenize_with_text;
use itertools::Itertools;
use rustc_ast::token::CommentKind;
use rustc_ast::{AttrKind, AttrStyle, Attribute, Crate, Item, ItemKind, ModKind, NodeId};
use rustc_ast::{AssocItemKind, AttrKind, AttrStyle, Attribute, Crate, Item, ItemKind, ModKind, NodeId};
use rustc_errors::{Applicability, Diag, SuggestionStyle};
use rustc_lexer::TokenKind;
use rustc_lint::{EarlyContext, EarlyLintPass, LintContext};
use rustc_session::impl_lint_pass;
use rustc_span::symbol::kw;
use rustc_span::{BytePos, ExpnKind, InnerSpan, Span, SpanData, Symbol};
use rustc_span::{BytePos, ExpnKind, Ident, InnerSpan, Span, SpanData, Symbol};

declare_clippy_lint! {
/// ### What it does
Expand Down Expand Up @@ -367,38 +367,26 @@ impl EmptyLineAfter {
);
}
}
}

impl EarlyLintPass for EmptyLineAfter {
fn check_crate(&mut self, _: &EarlyContext<'_>, krate: &Crate) {
self.items.push(ItemInfo {
kind: "crate",
name: kw::Crate,
span: krate.spans.inner_span.with_hi(krate.spans.inner_span.lo()),
mod_items: krate
.items
.iter()
.filter(|i| !matches!(i.span.ctxt().outer_expn_data().kind, ExpnKind::AstPass(_)))
.map(|i| i.id)
.collect::<Vec<_>>(),
});
}

fn check_item_post(&mut self, _: &EarlyContext<'_>, _: &Item) {
self.items.pop();
}

fn check_item(&mut self, cx: &EarlyContext<'_>, item: &Item) {
fn check_item_kind(
&mut self,
cx: &EarlyContext<'_>,
kind: &ItemKind,
ident: &Ident,
span: Span,
attrs: &[Attribute],
id: NodeId,
) {
self.items.push(ItemInfo {
kind: item.kind.descr(),
name: item.ident.name,
span: if item.span.contains(item.ident.span) {
item.span.with_hi(item.ident.span.hi())
kind: kind.descr(),
name: ident.name,
span: if span.contains(ident.span) {
span.with_hi(ident.span.hi())
} else {
item.span.with_hi(item.span.lo())
span.with_hi(span.lo())
},
mod_items: match item.kind {
ItemKind::Mod(_, ModKind::Loaded(ref items, _, _, _)) => items
mod_items: match kind {
ItemKind::Mod(_, ModKind::Loaded(items, _, _, _)) => items
.iter()
.filter(|i| !matches!(i.span.ctxt().outer_expn_data().kind, ExpnKind::AstPass(_)))
.map(|i| i.id)
Expand All @@ -407,8 +395,7 @@ impl EarlyLintPass for EmptyLineAfter {
},
});

let mut outer = item
.attrs
let mut outer = attrs
.iter()
.filter(|attr| attr.style == AttrStyle::Outer && !attr.span.from_expansion())
.map(|attr| Stop::from_attr(cx, attr))
Expand Down Expand Up @@ -448,6 +435,52 @@ impl EarlyLintPass for EmptyLineAfter {
}
}

self.check_gaps(cx, &gaps, item.id);
self.check_gaps(cx, &gaps, id);
}
}

impl EarlyLintPass for EmptyLineAfter {
fn check_crate(&mut self, _: &EarlyContext<'_>, krate: &Crate) {
self.items.push(ItemInfo {
kind: "crate",
name: kw::Crate,
span: krate.spans.inner_span.with_hi(krate.spans.inner_span.lo()),
mod_items: krate
.items
.iter()
.filter(|i| !matches!(i.span.ctxt().outer_expn_data().kind, ExpnKind::AstPass(_)))
.map(|i| i.id)
.collect::<Vec<_>>(),
});
}

fn check_item_post(&mut self, _: &EarlyContext<'_>, _: &Item) {
self.items.pop();
}

fn check_impl_item(&mut self, cx: &EarlyContext<'_>, item: &Item<AssocItemKind>) {
self.check_item_kind(
cx,
&item.kind.clone().into(),
&item.ident,
item.span,
&item.attrs,
item.id,
);
}

fn check_trait_item(&mut self, cx: &EarlyContext<'_>, item: &Item<AssocItemKind>) {
self.check_item_kind(
cx,
&item.kind.clone().into(),
&item.ident,
item.span,
&item.attrs,
item.id,
);
}

fn check_item(&mut self, cx: &EarlyContext<'_>, item: &Item) {
self.check_item_kind(cx, &item.kind, &item.ident, item.span, &item.attrs, item.id);
}
}
14 changes: 14 additions & 0 deletions tests/ui/empty_line_after/doc_comments.1.fixed
Original file line number Diff line number Diff line change
Expand Up @@ -132,4 +132,18 @@ pub struct BlockComment;
))]
fn empty_line_in_cfg_attr() {}

trait Foo {
fn bar();
}

impl Foo for LineComment {
/// Returns an `Option<Month>` from a i64, assuming a 1-index, January = 1.
///
/// `Month::from_i64(n: i64)`: | `1` | `2` | ... | `12`
/// ---------------------------| -------------------- | --------------------- | ... | -----
/// ``: | Some(Month::January) | Some(Month::February) | ... |
/// Some(Month::December)
fn bar() {}
}

fn main() {}
14 changes: 14 additions & 0 deletions tests/ui/empty_line_after/doc_comments.2.fixed
Original file line number Diff line number Diff line change
Expand Up @@ -141,4 +141,18 @@ pub struct BlockComment;
))]
fn empty_line_in_cfg_attr() {}

trait Foo {
fn bar();
}

impl Foo for LineComment {
/// Returns an `Option<Month>` from a i64, assuming a 1-index, January = 1.
///
/// `Month::from_i64(n: i64)`: | `1` | `2` | ... | `12`
/// ---------------------------| -------------------- | --------------------- | ... | -----
/// ``: | Some(Month::January) | Some(Month::February) | ... |
/// Some(Month::December)
fn bar() {}
}

fn main() {}
15 changes: 15 additions & 0 deletions tests/ui/empty_line_after/doc_comments.rs
Original file line number Diff line number Diff line change
Expand Up @@ -144,4 +144,19 @@ pub struct BlockComment;
))]
fn empty_line_in_cfg_attr() {}

trait Foo {
fn bar();
}

impl Foo for LineComment {
/// Returns an `Option<Month>` from a i64, assuming a 1-index, January = 1.
///
/// `Month::from_i64(n: i64)`: | `1` | `2` | ... | `12`
/// ---------------------------| -------------------- | --------------------- | ... | -----
/// ``: | Some(Month::January) | Some(Month::February) | ... |
/// Some(Month::December)
fn bar() {}
}

fn main() {}
13 changes: 12 additions & 1 deletion tests/ui/empty_line_after/doc_comments.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -171,5 +171,16 @@ help: if the doc comment should not document `new_code2` comment it out
LL | // /// Docs for `old_code2`
| ++

error: aborting due to 10 previous errors
error: empty line after doc comment
--> tests/ui/empty_line_after/doc_comments.rs:157:5
|
LL | / /// Some(Month::December)
LL | |
| |_^
LL | fn bar() {}
| ------ the comment documents this function
|
= help: if the empty line is unintentional remove it

error: aborting due to 11 previous errors

0 comments on commit 7783dcd

Please sign in to comment.