-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add new lint
doc_include_without_cfg
- Loading branch information
1 parent
73bad36
commit ffbed4d
Showing
6 changed files
with
68 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
use clippy_utils::diagnostics::span_lint_and_sugg; | ||
use clippy_utils::source::snippet_opt; | ||
use rustc_ast::{AttrArgs, AttrArgsEq, AttrKind, AttrStyle, Attribute}; | ||
use rustc_errors::Applicability; | ||
use rustc_lint::LateContext; | ||
use rustc_span::sym; | ||
|
||
use super::DOC_INCLUDE_WITHOUT_CFG; | ||
|
||
pub fn check(cx: &LateContext<'_>, attrs: &[Attribute]) { | ||
for attr in attrs { | ||
if let AttrKind::Normal(ref normal) = attr.kind | ||
&& normal.item.path == sym::doc | ||
&& let AttrArgs::Eq(_, AttrArgsEq::Hir(ref meta)) = normal.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. | ||
&& let Some(snippet) = snippet_opt(cx, attr.span) | ||
// We cannot remove this because a `#[doc = include_str!("...")]` attribute can occupy | ||
// several lines. | ||
&& let Some(start) = snippet.find('[') | ||
&& let Some(end) = snippet.rfind(']') | ||
{ | ||
let snippet = &snippet[start + 1..end]; | ||
span_lint_and_sugg( | ||
cx, | ||
DOC_INCLUDE_WITHOUT_CFG, | ||
attr.span, | ||
"included a file in documentation unconditionally", | ||
"use `cfg_attr(doc, doc = \"...\")`", | ||
format!( | ||
"#{}[cfg_attr(doc, {snippet})]", | ||
if attr.style == AttrStyle::Inner { "!" } else { "" } | ||
), | ||
Applicability::MachineApplicable, | ||
); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters