Skip to content

Commit 43ecf8e

Browse files
committed
Auto merge of rust-lang#10953 - KisaragiEffective:missing_panics_doc_trigger_on_expect, r=dswij
[`missing_panics_doc`]: pickup expect method close rust-lang#10240 *Please write a short comment explaining your change (or "none" for internal only changes)* changelog: [`missing_panics_doc`]: pickup expect method
2 parents 87b5f89 + 73c0c14 commit 43ecf8e

File tree

6 files changed

+115
-3
lines changed

6 files changed

+115
-3
lines changed

Diff for: clippy_dev/src/fmt.rs

+1
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ struct FmtContext {
3535
}
3636

3737
// the "main" function of cargo dev fmt
38+
#[allow(clippy::missing_panics_doc)]
3839
pub fn run(check: bool, verbose: bool) {
3940
fn try_run(context: &FmtContext) -> Result<bool, CliError> {
4041
let mut success = true;

Diff for: clippy_dev/src/lib.rs

+6
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,10 @@ static CARGO_CLIPPY_EXE: &str = "cargo-clippy";
2929
static CARGO_CLIPPY_EXE: &str = "cargo-clippy.exe";
3030

3131
/// Returns the path to the `cargo-clippy` binary
32+
///
33+
/// # Panics
34+
///
35+
/// Panics if the path of current executable could not be retrieved.
3236
#[must_use]
3337
pub fn cargo_clippy_path() -> PathBuf {
3438
let mut path = std::env::current_exe().expect("failed to get current executable name");
@@ -61,6 +65,8 @@ pub fn clippy_project_root() -> PathBuf {
6165
panic!("error: Can't determine root of project. Please run inside a Clippy working dir.");
6266
}
6367

68+
/// # Panics
69+
/// Panics if given command result was failed.
6470
pub fn exit_if_err(status: io::Result<ExitStatus>) {
6571
match status.expect("failed to run command").code() {
6672
Some(0) => {},

Diff for: clippy_dev/src/new_lint.rs

+1
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ impl<T> Context for io::Result<T> {
3636
/// # Errors
3737
///
3838
/// This function errors out if the files couldn't be created or written to.
39+
#[allow(clippy::missing_panics_doc)]
3940
pub fn create(
4041
pass: &String,
4142
lint_name: Option<&String>,

Diff for: clippy_lints/src/doc.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -916,8 +916,8 @@ impl<'a, 'tcx> Visitor<'tcx> for FindPanicUnwrap<'a, 'tcx> {
916916
}
917917
}
918918

919-
// check for `unwrap`
920-
if let Some(arglists) = method_chain_args(expr, &["unwrap"]) {
919+
// check for `unwrap` and `expect` for both `Option` and `Result`
920+
if let Some(arglists) = method_chain_args(expr, &["unwrap"]).or(method_chain_args(expr, &["expect"])) {
921921
let receiver_ty = self.typeck_results.expr_ty(arglists[0].0).peel_refs();
922922
if is_type_diagnostic_item(self.cx, receiver_ty, sym::Option)
923923
|| is_type_diagnostic_item(self.cx, receiver_ty, sym::Result)

Diff for: tests/ui/missing_panics_doc.rs

+32
Original file line numberDiff line numberDiff line change
@@ -151,3 +151,35 @@ pub fn debug_assertions() {
151151
debug_assert_eq!(1, 2);
152152
debug_assert_ne!(1, 2);
153153
}
154+
155+
// all function must be triggered the lint.
156+
// `pub` is required, because the lint does not consider unreachable items
157+
pub mod issue10240 {
158+
pub fn option_unwrap<T>(v: &[T]) -> &T {
159+
let o: Option<&T> = v.last();
160+
o.unwrap()
161+
}
162+
163+
pub fn option_expect<T>(v: &[T]) -> &T {
164+
let o: Option<&T> = v.last();
165+
o.expect("passed an empty thing")
166+
}
167+
168+
pub fn result_unwrap<T>(v: &[T]) -> &T {
169+
let res: Result<&T, &str> = v.last().ok_or("oh noes");
170+
res.unwrap()
171+
}
172+
173+
pub fn result_expect<T>(v: &[T]) -> &T {
174+
let res: Result<&T, &str> = v.last().ok_or("oh noes");
175+
res.expect("passed an empty thing")
176+
}
177+
178+
pub fn last_unwrap(v: &[u32]) -> u32 {
179+
*v.last().unwrap()
180+
}
181+
182+
pub fn last_expect(v: &[u32]) -> u32 {
183+
*v.last().expect("passed an empty thing")
184+
}
185+
}

Diff for: tests/ui/missing_panics_doc.stderr

+73-1
Original file line numberDiff line numberDiff line change
@@ -83,5 +83,77 @@ note: first possible panic found here
8383
LL | assert_ne!(x, 0);
8484
| ^^^^^^^^^^^^^^^^
8585

86-
error: aborting due to 7 previous errors
86+
error: docs for function which may panic missing `# Panics` section
87+
--> $DIR/missing_panics_doc.rs:158:5
88+
|
89+
LL | pub fn option_unwrap<T>(v: &[T]) -> &T {
90+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
91+
|
92+
note: first possible panic found here
93+
--> $DIR/missing_panics_doc.rs:160:9
94+
|
95+
LL | o.unwrap()
96+
| ^^^^^^^^^^
97+
98+
error: docs for function which may panic missing `# Panics` section
99+
--> $DIR/missing_panics_doc.rs:163:5
100+
|
101+
LL | pub fn option_expect<T>(v: &[T]) -> &T {
102+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
103+
|
104+
note: first possible panic found here
105+
--> $DIR/missing_panics_doc.rs:165:9
106+
|
107+
LL | o.expect("passed an empty thing")
108+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
109+
110+
error: docs for function which may panic missing `# Panics` section
111+
--> $DIR/missing_panics_doc.rs:168:5
112+
|
113+
LL | pub fn result_unwrap<T>(v: &[T]) -> &T {
114+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
115+
|
116+
note: first possible panic found here
117+
--> $DIR/missing_panics_doc.rs:170:9
118+
|
119+
LL | res.unwrap()
120+
| ^^^^^^^^^^^^
121+
122+
error: docs for function which may panic missing `# Panics` section
123+
--> $DIR/missing_panics_doc.rs:173:5
124+
|
125+
LL | pub fn result_expect<T>(v: &[T]) -> &T {
126+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
127+
|
128+
note: first possible panic found here
129+
--> $DIR/missing_panics_doc.rs:175:9
130+
|
131+
LL | res.expect("passed an empty thing")
132+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
133+
134+
error: docs for function which may panic missing `# Panics` section
135+
--> $DIR/missing_panics_doc.rs:178:5
136+
|
137+
LL | pub fn last_unwrap(v: &[u32]) -> u32 {
138+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
139+
|
140+
note: first possible panic found here
141+
--> $DIR/missing_panics_doc.rs:179:10
142+
|
143+
LL | *v.last().unwrap()
144+
| ^^^^^^^^^^^^^^^^^
145+
146+
error: docs for function which may panic missing `# Panics` section
147+
--> $DIR/missing_panics_doc.rs:182:5
148+
|
149+
LL | pub fn last_expect(v: &[u32]) -> u32 {
150+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
151+
|
152+
note: first possible panic found here
153+
--> $DIR/missing_panics_doc.rs:183:10
154+
|
155+
LL | *v.last().expect("passed an empty thing")
156+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
157+
158+
error: aborting due to 13 previous errors
87159

0 commit comments

Comments
 (0)