Skip to content

Commit 844765c

Browse files
committed
Auto merge of #4603 - rust-lang:needless-doc-main, r=flip1995
New lint: needless_doc_main changelog: Add `needless_doc_main` lint
2 parents 83f90aa + 23a9c02 commit 844765c

File tree

5 files changed

+55
-12
lines changed

5 files changed

+55
-12
lines changed

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -1105,6 +1105,7 @@ Released 2018-09-13
11051105
[`needless_borrowed_reference`]: https://rust-lang.github.io/rust-clippy/master/index.html#needless_borrowed_reference
11061106
[`needless_collect`]: https://rust-lang.github.io/rust-clippy/master/index.html#needless_collect
11071107
[`needless_continue`]: https://rust-lang.github.io/rust-clippy/master/index.html#needless_continue
1108+
[`needless_doctest_main`]: https://rust-lang.github.io/rust-clippy/master/index.html#needless_doctest_main
11081109
[`needless_lifetimes`]: https://rust-lang.github.io/rust-clippy/master/index.html#needless_lifetimes
11091110
[`needless_pass_by_value`]: https://rust-lang.github.io/rust-clippy/master/index.html#needless_pass_by_value
11101111
[`needless_range_loop`]: https://rust-lang.github.io/rust-clippy/master/index.html#needless_range_loop

README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
A collection of lints to catch common mistakes and improve your [Rust](https://github.com/rust-lang/rust) code.
88

9-
[There are 317 lints included in this crate!](https://rust-lang.github.io/rust-clippy/master/index.html)
9+
[There are 318 lints included in this crate!](https://rust-lang.github.io/rust-clippy/master/index.html)
1010

1111
We have a bunch of lint categories to allow you to choose how much Clippy is supposed to ~~annoy~~ help you:
1212

clippy_lints/src/doc.rs

+43-10
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,34 @@ declare_clippy_lint! {
6868
"`pub unsafe fn` without `# Safety` docs"
6969
}
7070

71+
declare_clippy_lint! {
72+
/// **What it does:** Checks for `fn main() { .. }` in doctests
73+
///
74+
/// **Why is this bad?** The test can be shorter (and likely more readable)
75+
/// if the `fn main()` is left implicit.
76+
///
77+
/// **Known problems:** None.
78+
///
79+
/// **Examples:**
80+
/// ``````rust
81+
/// /// An example of a doctest with a `main()` function
82+
/// ///
83+
/// /// # Examples
84+
/// ///
85+
/// /// ```
86+
/// /// fn main() {
87+
/// /// // this needs not be in an `fn`
88+
/// /// }
89+
/// /// ```
90+
/// fn needless_main() {
91+
/// unimplemented!();
92+
/// }
93+
/// ``````
94+
pub NEEDLESS_DOCTEST_MAIN,
95+
style,
96+
"presence of `fn main() {` in code examples"
97+
}
98+
7199
#[allow(clippy::module_name_repetitions)]
72100
#[derive(Clone)]
73101
pub struct DocMarkdown {
@@ -80,7 +108,7 @@ impl DocMarkdown {
80108
}
81109
}
82110

83-
impl_lint_pass!(DocMarkdown => [DOC_MARKDOWN, MISSING_SAFETY_DOC]);
111+
impl_lint_pass!(DocMarkdown => [DOC_MARKDOWN, MISSING_SAFETY_DOC, NEEDLESS_DOCTEST_MAIN]);
84112

85113
impl EarlyLintPass for DocMarkdown {
86114
fn check_crate(&mut self, cx: &EarlyContext<'_>, krate: &ast::Crate) {
@@ -245,17 +273,16 @@ fn check_doc<'a, Events: Iterator<Item = (pulldown_cmark::Event<'a>, Range<usize
245273
continue;
246274
}
247275
safety_header |= in_heading && text.trim() == "Safety";
248-
if !in_code {
249-
let index = match spans.binary_search_by(|c| c.0.cmp(&range.start)) {
250-
Ok(o) => o,
251-
Err(e) => e - 1,
252-
};
253-
254-
let (begin, span) = spans[index];
255-
276+
let index = match spans.binary_search_by(|c| c.0.cmp(&range.start)) {
277+
Ok(o) => o,
278+
Err(e) => e - 1,
279+
};
280+
let (begin, span) = spans[index];
281+
if in_code {
282+
check_code(cx, &text, span);
283+
} else {
256284
// Adjust for the beginning of the current `Event`
257285
let span = span.with_lo(span.lo() + BytePos::from_usize(range.start - begin));
258-
259286
check_text(cx, valid_idents, &text, span);
260287
}
261288
},
@@ -264,6 +291,12 @@ fn check_doc<'a, Events: Iterator<Item = (pulldown_cmark::Event<'a>, Range<usize
264291
safety_header
265292
}
266293

294+
fn check_code(cx: &EarlyContext<'_>, text: &str, span: Span) {
295+
if text.contains("fn main() {") {
296+
span_lint(cx, NEEDLESS_DOCTEST_MAIN, span, "needless `fn main` in doctest");
297+
}
298+
}
299+
267300
fn check_text(cx: &EarlyContext<'_>, valid_idents: &FxHashSet<String>, text: &str, span: Span) {
268301
for word in text.split(|c: char| c.is_whitespace() || c == '\'') {
269302
// Trim punctuation as in `some comment (see foo::bar).`

clippy_lints/src/lib.rs

+2
Original file line numberDiff line numberDiff line change
@@ -713,6 +713,7 @@ pub fn register_plugins(reg: &mut rustc_driver::plugin::Registry<'_>, conf: &Con
713713
copies::IF_SAME_THEN_ELSE,
714714
derive::DERIVE_HASH_XOR_EQ,
715715
doc::MISSING_SAFETY_DOC,
716+
doc::NEEDLESS_DOCTEST_MAIN,
716717
double_comparison::DOUBLE_COMPARISONS,
717718
double_parens::DOUBLE_PARENS,
718719
drop_bounds::DROP_BOUNDS,
@@ -937,6 +938,7 @@ pub fn register_plugins(reg: &mut rustc_driver::plugin::Registry<'_>, conf: &Con
937938
collapsible_if::COLLAPSIBLE_IF,
938939
comparison_chain::COMPARISON_CHAIN,
939940
doc::MISSING_SAFETY_DOC,
941+
doc::NEEDLESS_DOCTEST_MAIN,
940942
enum_variants::ENUM_VARIANT_NAMES,
941943
enum_variants::MODULE_INCEPTION,
942944
eq_op::OP_REF,

src/lintlist/mod.rs

+8-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ pub use lint::Lint;
66
pub use lint::LINT_LEVELS;
77

88
// begin lint list, do not remove this comment, it’s used in `update_lints`
9-
pub const ALL_LINTS: [Lint; 317] = [
9+
pub const ALL_LINTS: [Lint; 318] = [
1010
Lint {
1111
name: "absurd_extreme_comparisons",
1212
group: "correctness",
@@ -1225,6 +1225,13 @@ pub const ALL_LINTS: [Lint; 317] = [
12251225
deprecation: None,
12261226
module: "needless_continue",
12271227
},
1228+
Lint {
1229+
name: "needless_doctest_main",
1230+
group: "style",
1231+
desc: "presence of `fn main() {` in code examples",
1232+
deprecation: None,
1233+
module: "doc",
1234+
},
12281235
Lint {
12291236
name: "needless_lifetimes",
12301237
group: "complexity",

0 commit comments

Comments
 (0)