Fix rustdoc and clippy

This commit is contained in:
Jana Dönszelmann 2025-02-09 22:49:57 +01:00
parent 15820ecc2e
commit 443b0f5ccf
24 changed files with 118 additions and 112 deletions

View file

@ -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.

View file

@ -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::{
@ -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 {
@ -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))

View file

@ -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;
@ -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
}

View file

@ -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;
@ -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
@ -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 {