Skip to content

Commit 0ee393c

Browse files
committed
Add tests and improve checks.
1 parent 0b168c6 commit 0ee393c

File tree

4 files changed

+83
-16
lines changed

4 files changed

+83
-16
lines changed

clippy_lints/src/doc.rs

Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use crate::utils::{is_entrypoint_fn, match_type, paths, return_ty, span_lint};
1+
use crate::utils::{get_trait_def_id, implements_trait, is_entrypoint_fn, match_type, paths, return_ty, span_lint};
22
use itertools::Itertools;
33
use rustc::lint::in_external_macro;
44
use rustc::ty::TyKind;
@@ -223,19 +223,24 @@ fn lint_for_missing_headers<'a, 'tcx>(
223223
"docs for function returning `Result` missing `# Errors` section",
224224
);
225225
} else {
226-
use TyKind::*;
227226
let def_id = cx.tcx.hir().local_def_id(hir_id);
228227
let mir = cx.tcx.optimized_mir(def_id);
229-
if let Opaque(_, subs) = mir.return_ty().kind {
230-
if let Some(ty) = subs.types().next() {
231-
if let Generator(_, subs, _) = ty.kind {
232-
if match_type(cx, subs.as_generator().return_ty(def_id, cx.tcx), &paths::RESULT) {
233-
span_lint(
234-
cx,
235-
MISSING_ERRORS_DOC,
236-
span,
237-
"docs for function returning `Result` missing `# Errors` section",
238-
);
228+
if let Some(future) = get_trait_def_id(cx, &paths::FUTURE) {
229+
if implements_trait(cx, mir.return_ty(), future, &[]) {
230+
use TyKind::*;
231+
232+
if let Opaque(_, subs) = mir.return_ty().kind {
233+
if let Some(ty) = subs.types().next() {
234+
if let Generator(_, subs, _) = ty.kind {
235+
if match_type(cx, subs.as_generator().return_ty(def_id, cx.tcx), &paths::RESULT) {
236+
span_lint(
237+
cx,
238+
MISSING_ERRORS_DOC,
239+
span,
240+
"docs for function returning `Result` missing `# Errors` section",
241+
);
242+
}
243+
}
239244
}
240245
}
241246
}

clippy_lints/src/utils/paths.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ pub const FMT_ARGUMENTS_NEW_V1_FORMATTED: [&str; 4] = ["core", "fmt", "Arguments
3636
pub const FMT_ARGUMENTV1_NEW: [&str; 4] = ["core", "fmt", "ArgumentV1", "new"];
3737
pub const FROM_FROM: [&str; 4] = ["core", "convert", "From", "from"];
3838
pub const FROM_TRAIT: [&str; 3] = ["core", "convert", "From"];
39+
pub const FUTURE: [&str; 3] = ["std", "future", "Future"];
3940
pub const HASH: [&str; 2] = ["hash", "Hash"];
4041
pub const HASHMAP: [&str; 5] = ["std", "collections", "hash", "map", "HashMap"];
4142
pub const HASHMAP_ENTRY: [&str; 5] = ["std", "collections", "hash", "map", "Entry"];

tests/ui/doc_errors.rs

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
// compile-flags: --edition 2018
12
#![warn(clippy::missing_errors_doc)]
23

34
use std::io;
@@ -6,22 +7,42 @@ pub fn pub_fn_missing_errors_header() -> Result<(), ()> {
67
unimplemented!();
78
}
89

10+
pub async fn async_pub_fn_missing_errors_header() -> Result<(), ()> {
11+
unimplemented!();
12+
}
13+
914
/// This is not sufficiently documented.
1015
pub fn pub_fn_returning_io_result() -> io::Result<()> {
1116
unimplemented!();
1217
}
1318

19+
/// This is not sufficiently documented.
20+
pub async fn async_pub_fn_returning_io_result() -> io::Result<()> {
21+
unimplemented!();
22+
}
23+
1424
/// # Errors
1525
/// A description of the errors goes here.
1626
pub fn pub_fn_with_errors_header() -> Result<(), ()> {
1727
unimplemented!();
1828
}
1929

30+
/// # Errors
31+
/// A description of the errors goes here.
32+
pub async fn async_pub_fn_with_errors_header() -> Result<(), ()> {
33+
unimplemented!();
34+
}
35+
2036
/// This function doesn't require the documentation because it is private
2137
fn priv_fn_missing_errors_header() -> Result<(), ()> {
2238
unimplemented!();
2339
}
2440

41+
/// This function doesn't require the documentation because it is private
42+
async fn async_priv_fn_missing_errors_header() -> Result<(), ()> {
43+
unimplemented!();
44+
}
45+
2546
pub struct Struct1;
2647

2748
impl Struct1 {
@@ -30,16 +51,32 @@ impl Struct1 {
3051
unimplemented!();
3152
}
3253

54+
/// This is not sufficiently documented.
55+
pub async fn async_pub_method_missing_errors_header() -> Result<(), ()> {
56+
unimplemented!();
57+
}
58+
3359
/// # Errors
3460
/// A description of the errors goes here.
3561
pub fn pub_method_with_errors_header() -> Result<(), ()> {
3662
unimplemented!();
3763
}
3864

65+
/// # Errors
66+
/// A description of the errors goes here.
67+
pub async fn async_pub_method_with_errors_header() -> Result<(), ()> {
68+
unimplemented!();
69+
}
70+
3971
/// This function doesn't require the documentation because it is private.
4072
fn priv_method_missing_errors_header() -> Result<(), ()> {
4173
unimplemented!();
4274
}
75+
76+
/// This function doesn't require the documentation because it is private.
77+
async fn async_priv_method_missing_errors_header() -> Result<(), ()> {
78+
unimplemented!();
79+
}
4380
}
4481

4582
pub trait Trait1 {

tests/ui/doc_errors.stderr

Lines changed: 28 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
error: docs for function returning `Result` missing `# Errors` section
2-
--> $DIR/doc_errors.rs:5:1
2+
--> $DIR/doc_errors.rs:6:1
33
|
44
LL | / pub fn pub_fn_missing_errors_header() -> Result<(), ()> {
55
LL | | unimplemented!();
@@ -11,24 +11,48 @@ LL | | }
1111
error: docs for function returning `Result` missing `# Errors` section
1212
--> $DIR/doc_errors.rs:10:1
1313
|
14+
LL | / pub async fn async_pub_fn_missing_errors_header() -> Result<(), ()> {
15+
LL | | unimplemented!();
16+
LL | | }
17+
| |_^
18+
19+
error: docs for function returning `Result` missing `# Errors` section
20+
--> $DIR/doc_errors.rs:15:1
21+
|
1422
LL | / pub fn pub_fn_returning_io_result() -> io::Result<()> {
1523
LL | | unimplemented!();
1624
LL | | }
1725
| |_^
1826

1927
error: docs for function returning `Result` missing `# Errors` section
20-
--> $DIR/doc_errors.rs:29:5
28+
--> $DIR/doc_errors.rs:20:1
29+
|
30+
LL | / pub async fn async_pub_fn_returning_io_result() -> io::Result<()> {
31+
LL | | unimplemented!();
32+
LL | | }
33+
| |_^
34+
35+
error: docs for function returning `Result` missing `# Errors` section
36+
--> $DIR/doc_errors.rs:50:5
2137
|
2238
LL | / pub fn pub_method_missing_errors_header() -> Result<(), ()> {
2339
LL | | unimplemented!();
2440
LL | | }
2541
| |_____^
2642

2743
error: docs for function returning `Result` missing `# Errors` section
28-
--> $DIR/doc_errors.rs:47:5
44+
--> $DIR/doc_errors.rs:55:5
45+
|
46+
LL | / pub async fn async_pub_method_missing_errors_header() -> Result<(), ()> {
47+
LL | | unimplemented!();
48+
LL | | }
49+
| |_____^
50+
51+
error: docs for function returning `Result` missing `# Errors` section
52+
--> $DIR/doc_errors.rs:84:5
2953
|
3054
LL | fn trait_method_missing_errors_header() -> Result<(), ()>;
3155
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
3256

33-
error: aborting due to 4 previous errors
57+
error: aborting due to 7 previous errors
3458

0 commit comments

Comments
 (0)