Skip to content

[missing_panics_doc]: pickup expect method #10953

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions clippy_dev/src/fmt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ struct FmtContext {
}

// the "main" function of cargo dev fmt
#[allow(clippy::missing_panics_doc)]
pub fn run(check: bool, verbose: bool) {
fn try_run(context: &FmtContext) -> Result<bool, CliError> {
let mut success = true;
Expand Down
6 changes: 6 additions & 0 deletions clippy_dev/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,10 @@ static CARGO_CLIPPY_EXE: &str = "cargo-clippy";
static CARGO_CLIPPY_EXE: &str = "cargo-clippy.exe";

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

/// # Panics
/// Panics if given command result was failed.
pub fn exit_if_err(status: io::Result<ExitStatus>) {
match status.expect("failed to run command").code() {
Some(0) => {},
Expand Down
1 change: 1 addition & 0 deletions clippy_dev/src/new_lint.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ impl<T> Context for io::Result<T> {
/// # Errors
///
/// This function errors out if the files couldn't be created or written to.
#[allow(clippy::missing_panics_doc)]
pub fn create(
pass: &String,
lint_name: Option<&String>,
Expand Down
4 changes: 2 additions & 2 deletions clippy_lints/src/doc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -916,8 +916,8 @@ impl<'a, 'tcx> Visitor<'tcx> for FindPanicUnwrap<'a, 'tcx> {
}
}

// check for `unwrap`
if let Some(arglists) = method_chain_args(expr, &["unwrap"]) {
// check for `unwrap` and `expect` for both `Option` and `Result`
if let Some(arglists) = method_chain_args(expr, &["unwrap"]).or(method_chain_args(expr, &["expect"])) {
let receiver_ty = self.typeck_results.expr_ty(arglists[0].0).peel_refs();
if is_type_diagnostic_item(self.cx, receiver_ty, sym::Option)
|| is_type_diagnostic_item(self.cx, receiver_ty, sym::Result)
Expand Down
32 changes: 32 additions & 0 deletions tests/ui/missing_panics_doc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -151,3 +151,35 @@ pub fn debug_assertions() {
debug_assert_eq!(1, 2);
debug_assert_ne!(1, 2);
}

// all function must be triggered the lint.
// `pub` is required, because the lint does not consider unreachable items
pub mod issue10240 {
pub fn option_unwrap<T>(v: &[T]) -> &T {
let o: Option<&T> = v.last();
o.unwrap()
}

pub fn option_expect<T>(v: &[T]) -> &T {
let o: Option<&T> = v.last();
o.expect("passed an empty thing")
}

pub fn result_unwrap<T>(v: &[T]) -> &T {
let res: Result<&T, &str> = v.last().ok_or("oh noes");
res.unwrap()
}

pub fn result_expect<T>(v: &[T]) -> &T {
let res: Result<&T, &str> = v.last().ok_or("oh noes");
res.expect("passed an empty thing")
}

pub fn last_unwrap(v: &[u32]) -> u32 {
*v.last().unwrap()
}

pub fn last_expect(v: &[u32]) -> u32 {
*v.last().expect("passed an empty thing")
}
}
74 changes: 73 additions & 1 deletion tests/ui/missing_panics_doc.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -83,5 +83,77 @@ note: first possible panic found here
LL | assert_ne!(x, 0);
| ^^^^^^^^^^^^^^^^

error: aborting due to 7 previous errors
error: docs for function which may panic missing `# Panics` section
--> $DIR/missing_panics_doc.rs:158:5
|
LL | pub fn option_unwrap<T>(v: &[T]) -> &T {
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
note: first possible panic found here
--> $DIR/missing_panics_doc.rs:160:9
|
LL | o.unwrap()
| ^^^^^^^^^^

error: docs for function which may panic missing `# Panics` section
--> $DIR/missing_panics_doc.rs:163:5
|
LL | pub fn option_expect<T>(v: &[T]) -> &T {
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
note: first possible panic found here
--> $DIR/missing_panics_doc.rs:165:9
|
LL | o.expect("passed an empty thing")
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

error: docs for function which may panic missing `# Panics` section
--> $DIR/missing_panics_doc.rs:168:5
|
LL | pub fn result_unwrap<T>(v: &[T]) -> &T {
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
note: first possible panic found here
--> $DIR/missing_panics_doc.rs:170:9
|
LL | res.unwrap()
| ^^^^^^^^^^^^

error: docs for function which may panic missing `# Panics` section
--> $DIR/missing_panics_doc.rs:173:5
|
LL | pub fn result_expect<T>(v: &[T]) -> &T {
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
note: first possible panic found here
--> $DIR/missing_panics_doc.rs:175:9
|
LL | res.expect("passed an empty thing")
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

error: docs for function which may panic missing `# Panics` section
--> $DIR/missing_panics_doc.rs:178:5
|
LL | pub fn last_unwrap(v: &[u32]) -> u32 {
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
note: first possible panic found here
--> $DIR/missing_panics_doc.rs:179:10
|
LL | *v.last().unwrap()
| ^^^^^^^^^^^^^^^^^

error: docs for function which may panic missing `# Panics` section
--> $DIR/missing_panics_doc.rs:182:5
|
LL | pub fn last_expect(v: &[u32]) -> u32 {
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
note: first possible panic found here
--> $DIR/missing_panics_doc.rs:183:10
|
LL | *v.last().expect("passed an empty thing")
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

error: aborting due to 13 previous errors