Skip to content

Commit

Permalink
Fix rustdoc and clippy
Browse files Browse the repository at this point in the history
  • Loading branch information
jdonszelmann committed Feb 24, 2025
1 parent 15820ec commit 443b0f5
Show file tree
Hide file tree
Showing 24 changed files with 117 additions and 111 deletions.
2 changes: 1 addition & 1 deletion clippy_lints/src/attrs/inline_always.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ pub(super) fn check(cx: &LateContext<'_>, span: Span, name: Symbol, attrs: &[Att
span_lint(
cx,
INLINE_ALWAYS,
attr.span,
attr.span(),
format!("you have declared `#[inline(always)]` on `{name}`. This is usually a bad idea"),
);
}
Expand Down
42 changes: 17 additions & 25 deletions clippy_lints/src/attrs/repr_attributes.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use rustc_attr_parsing::{find_attr, AttributeKind, ReprAttr};
use rustc_hir::Attribute;
use rustc_lint::LateContext;
use rustc_span::{Span, sym};
use rustc_span::Span;

use clippy_utils::diagnostics::span_lint_and_then;
use clippy_utils::msrvs;
Expand All @@ -14,30 +15,21 @@ pub(super) fn check(cx: &LateContext<'_>, item_span: Span, attrs: &[Attribute],
}

fn check_packed(cx: &LateContext<'_>, item_span: Span, attrs: &[Attribute]) {
if let Some(items) = attrs.iter().find_map(|attr| {
if attr.ident().is_some_and(|ident| matches!(ident.name, sym::repr)) {
attr.meta_item_list()
} else {
None
if let Some(reprs) = find_attr!(attrs, AttributeKind::Repr(r) => r) {
let packed_span = reprs.iter().find(|(r, _)| matches!(r, ReprAttr::ReprPacked(..))).map(|(_, s)| *s);

if let Some(packed_span) = packed_span && !reprs.iter().any(|(x, _)| *x == ReprAttr::ReprC || *x == ReprAttr::ReprRust) {
span_lint_and_then(
cx,
REPR_PACKED_WITHOUT_ABI,
item_span,
"item uses `packed` representation without ABI-qualification",
|diag| {
diag.warn("unqualified `#[repr(packed)]` defaults to `#[repr(Rust, packed)]`, which has no stable ABI")
.help("qualify the desired ABI explicity via `#[repr(C, packed)]` or `#[repr(Rust, packed)]`")
.span_label(packed_span, "`packed` representation set here");
},
);
}
}) && let Some(packed) = items
.iter()
.find(|item| item.ident().is_some_and(|ident| matches!(ident.name, sym::packed)))
&& !items.iter().any(|item| {
item.ident()
.is_some_and(|ident| matches!(ident.name, sym::C | sym::Rust))
})
{
span_lint_and_then(
cx,
REPR_PACKED_WITHOUT_ABI,
item_span,
"item uses `packed` representation without ABI-qualification",
|diag| {
diag.warn("unqualified `#[repr(packed)]` defaults to `#[repr(Rust, packed)]`, which has no stable ABI")
.help("qualify the desired ABI explicity via `#[repr(C, packed)]` or `#[repr(Rust, packed)]`")
.span_label(packed.span(), "`packed` representation set here");
},
);
}
}
2 changes: 1 addition & 1 deletion clippy_lints/src/attrs/unnecessary_clippy_cfg.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ pub(super) fn check(
) {
if cfg_attr.has_name(sym::clippy)
&& let Some(ident) = behind_cfg_attr.ident()
&& Level::from_symbol(ident.name, Some(attr.id)).is_some()
&& Level::from_symbol(ident.name, || Some(attr.id)).is_some()
&& let Some(items) = behind_cfg_attr.meta_item_list()
{
let nb_items = items.len();
Expand Down
2 changes: 1 addition & 1 deletion clippy_lints/src/attrs/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ pub(super) fn is_word(nmi: &MetaItemInner, expected: Symbol) -> bool {
}

pub(super) fn is_lint_level(symbol: Symbol, attr_id: AttrId) -> bool {
Level::from_symbol(symbol, Some(attr_id)).is_some()
Level::from_symbol(symbol, || Some(attr_id)).is_some()
}

pub(super) fn is_relevant_item(cx: &LateContext<'_>, item: &Item<'_>) -> bool {
Expand Down
17 changes: 4 additions & 13 deletions clippy_lints/src/default_union_representation.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
use clippy_utils::diagnostics::span_lint_and_then;
use rustc_attr_parsing::{find_attr, AttributeKind, ReprAttr};
use rustc_hir::{HirId, Item, ItemKind};
use rustc_lint::{LateContext, LateLintPass};
use rustc_middle::ty::layout::LayoutOf;
use rustc_middle::ty::{self, FieldDef};
use rustc_session::declare_lint_pass;
use rustc_span::sym;

declare_clippy_lint! {
/// ### What it does
Expand Down Expand Up @@ -97,16 +97,7 @@ fn is_zst<'tcx>(cx: &LateContext<'tcx>, field: &FieldDef, args: ty::GenericArgsR
}

fn has_c_repr_attr(cx: &LateContext<'_>, hir_id: HirId) -> bool {
cx.tcx.hir().attrs(hir_id).iter().any(|attr| {
if attr.has_name(sym::repr) {
if let Some(items) = attr.meta_item_list() {
for item in items {
if item.is_word() && matches!(item.name_or_empty(), sym::C) {
return true;
}
}
}
}
false
})
let attrs = cx.tcx.hir().attrs(hir_id);

find_attr!(attrs, AttributeKind::Repr(r) if r.iter().any(|(x, _)| *x == ReprAttr::ReprC))
}
1 change: 1 addition & 0 deletions clippy_lints/src/disallowed_macros.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@

use clippy_config::Conf;
use clippy_config::types::create_disallowed_map;
use clippy_utils::diagnostics::{span_lint_and_then, span_lint_hir_and_then};
Expand Down
9 changes: 4 additions & 5 deletions clippy_lints/src/doc/include_in_doc_without_cfg.rs
Original file line number Diff line number Diff line change
@@ -1,18 +1,17 @@
use clippy_utils::diagnostics::span_lint_and_sugg;
use clippy_utils::source::snippet_opt;
use rustc_ast::AttrStyle;
use rustc_errors::Applicability;
use rustc_hir::{AttrArgs, AttrKind, Attribute};
use rustc_lint::LateContext;
use rustc_lint::EarlyContext;
use rustc_ast::{Attribute, AttrKind, AttrArgs, AttrStyle};

use super::DOC_INCLUDE_WITHOUT_CFG;

pub fn check(cx: &LateContext<'_>, attrs: &[Attribute]) {
pub fn check(cx: &EarlyContext<'_>, attrs: &[Attribute]) {
for attr in attrs {
if !attr.span.from_expansion()
&& let AttrKind::Normal(ref item) = attr.kind
&& attr.doc_str().is_some()
&& let AttrArgs::Eq { expr: meta, .. } = &item.args
&& let AttrArgs::Eq { expr: meta, .. } = &item.item.args
&& !attr.span.contains(meta.span)
// Since the `include_str` is already expanded at this point, we can only take the
// whole attribute snippet and then modify for our suggestion.
Expand Down
12 changes: 9 additions & 3 deletions clippy_lints/src/doc/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ use rustc_data_structures::fx::FxHashSet;
use rustc_errors::Applicability;
use rustc_hir::intravisit::{self, Visitor};
use rustc_hir::{AnonConst, Attribute, Expr, ImplItemKind, ItemKind, Node, Safety, TraitItemKind};
use rustc_lint::{LateContext, LateLintPass, LintContext};
use rustc_lint::{EarlyContext, EarlyLintPass, LateContext, LateLintPass, LintContext};
use rustc_middle::hir::nested_filter;
use rustc_middle::ty;
use rustc_resolve::rustdoc::{
Expand Down Expand Up @@ -577,6 +577,13 @@ impl_lint_pass!(Documentation => [
DOC_INCLUDE_WITHOUT_CFG,
]);


impl EarlyLintPass for Documentation {
fn check_attributes(&mut self, cx: &EarlyContext<'_>, attrs: &[rustc_ast::Attribute]) {
include_in_doc_without_cfg::check(cx, attrs);
}
}

impl<'tcx> LateLintPass<'tcx> for Documentation {
fn check_attributes(&mut self, cx: &LateContext<'tcx>, attrs: &'tcx [Attribute]) {
let Some(headers) = check_attrs(cx, &self.valid_idents, attrs) else {
Expand Down Expand Up @@ -704,14 +711,13 @@ fn check_attrs(cx: &LateContext<'_>, valid_idents: &FxHashSet<String>, attrs: &[
Some(("fake".into(), "fake".into()))
}

include_in_doc_without_cfg::check(cx, attrs);
if suspicious_doc_comments::check(cx, attrs) || is_doc_hidden(attrs) {
return None;
}

let (fragments, _) = attrs_to_doc_fragments(
attrs.iter().filter_map(|attr| {
if attr.span.in_external_macro(cx.sess().source_map()) {
if !attr.doc_str_and_comment_kind().is_some() || attr.span().in_external_macro(cx.sess().source_map()) {
None
} else {
Some((attr, None))
Expand Down
10 changes: 5 additions & 5 deletions clippy_lints/src/doc/suspicious_doc_comments.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ use rustc_ast::AttrStyle;
use rustc_ast::token::CommentKind;
use rustc_errors::Applicability;
use rustc_hir::Attribute;
use rustc_attr_parsing::AttributeKind;
use rustc_lint::LateContext;
use rustc_span::Span;

Expand Down Expand Up @@ -36,15 +37,14 @@ fn collect_doc_replacements(attrs: &[Attribute]) -> Vec<(Span, String)> {
attrs
.iter()
.filter_map(|attr| {
if let Some((sym, com_kind)) = attr.doc_str_and_comment_kind()
&& let AttrStyle::Outer = attr.style
&& let Some(com) = sym.as_str().strip_prefix('!')
if let Attribute::Parsed(AttributeKind::DocComment{ style: AttrStyle::Outer, kind, comment, ..}) = attr
&& let Some(com) = comment.as_str().strip_prefix('!')
{
let sugg = match com_kind {
let sugg = match kind {
CommentKind::Line => format!("//!{com}"),
CommentKind::Block => format!("/*!{com}*/"),
};
Some((attr.span, sugg))
Some((attr.span(), sugg))
} else {
None
}
Expand Down
9 changes: 5 additions & 4 deletions clippy_lints/src/doc/too_long_first_doc_paragraph.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use rustc_errors::Applicability;
use rustc_hir::{Attribute, Item, ItemKind};
use rustc_attr_parsing::AttributeKind;
use rustc_lint::LateContext;

use clippy_utils::diagnostics::span_lint_and_then;
Expand Down Expand Up @@ -43,9 +44,9 @@ pub(super) fn check(
let mut should_suggest_empty_doc = false;

for attr in attrs {
if let Some(doc) = attr.doc_str() {
spans.push(attr.span);
let doc = doc.as_str();
if let Attribute::Parsed(AttributeKind::DocComment {span, comment, ..}) = attr {
spans.push(span);
let doc = comment.as_str();
let doc = doc.trim();
if spans.len() == 1 {
// We make this suggestion only if the first doc line ends with a punctuation
Expand Down Expand Up @@ -78,7 +79,7 @@ pub(super) fn check(
&& let new_span = first_span.with_hi(second_span.lo()).with_lo(first_span.hi())
&& let Some(snippet) = snippet_opt(cx, new_span)
{
let Some(first) = snippet_opt(cx, first_span) else {
let Some(first) = snippet_opt(cx, *first_span) else {
return;
};
let Some(comment_form) = first.get(..3) else {
Expand Down
3 changes: 2 additions & 1 deletion clippy_lints/src/four_forward_slashes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,8 @@ impl<'tcx> LateLintPass<'tcx> for FourForwardSlashes {
.hir()
.attrs(item.hir_id())
.iter()
.fold(item.span.shrink_to_lo(), |span, attr| span.to(attr.span));
.filter(|i| i.is_doc_comment())
.fold(item.span.shrink_to_lo(), |span, attr| span.to(attr.span()));
let (Some(file), _, _, end_line, _) = sm.span_to_location_info(span) else {
return;
};
Expand Down
7 changes: 4 additions & 3 deletions clippy_lints/src/functions/must_use.rs
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@ pub(super) fn check_trait_item<'tcx>(cx: &LateContext<'tcx>, item: &'tcx hir::Tr
}
}

// FIXME: needs to be an EARLY LINT. all attribute lints should be
#[allow(clippy::too_many_arguments)]
fn check_needless_must_use(
cx: &LateContext<'_>,
Expand All @@ -117,7 +118,7 @@ fn check_needless_must_use(
fn_header_span,
"this unit-returning function has a `#[must_use]` attribute",
|diag| {
diag.span_suggestion(attr.span, "remove the attribute", "", Applicability::MachineApplicable);
diag.span_suggestion(attr.span(), "remove the attribute", "", Applicability::MachineApplicable);
},
);
} else {
Expand All @@ -130,7 +131,7 @@ fn check_needless_must_use(
"this unit-returning function has a `#[must_use]` attribute",
|diag| {
let mut attrs_without_must_use = attrs.to_vec();
attrs_without_must_use.retain(|a| a.id != attr.id);
attrs_without_must_use.retain(|a| a.id() != attr.id());
let sugg_str = attrs_without_must_use
.iter()
.map(|a| {
Expand All @@ -143,7 +144,7 @@ fn check_needless_must_use(
.join(", ");

diag.span_suggestion(
attrs[0].span.with_hi(attrs[attrs.len() - 1].span.hi()),
attrs[0].span().with_hi(attrs[attrs.len() - 1].span().hi()),
"change these attributes to",
sugg_str,
Applicability::MachineApplicable,
Expand Down
2 changes: 1 addition & 1 deletion clippy_lints/src/inconsistent_struct_constructor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,7 @@ fn suggestion<'tcx>(

fn field_with_attrs_span(tcx: TyCtxt<'_>, field: &hir::ExprField<'_>) -> Span {
if let Some(attr) = tcx.hir().attrs(field.hir_id).first() {
field.span.with_lo(attr.span.lo())
field.span.with_lo(attr.span().lo())
} else {
field.span
}
Expand Down
4 changes: 2 additions & 2 deletions clippy_lints/src/inline_fn_without_body.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,10 +42,10 @@ impl<'tcx> LateLintPass<'tcx> for InlineFnWithoutBody {
span_lint_and_then(
cx,
INLINE_FN_WITHOUT_BODY,
attr.span,
attr.span(),
format!("use of `#[inline]` on trait method `{}` which has no body", item.ident),
|diag| {
diag.suggest_remove_item(cx, attr.span, "remove", Applicability::MachineApplicable);
diag.suggest_remove_item(cx, attr.span(), "remove", Applicability::MachineApplicable);
},
);
}
Expand Down
Loading

0 comments on commit 443b0f5

Please sign in to comment.