Auto merge of #10953 - KisaragiEffective:missing_panics_doc_trigger_on_expect, r=dswij

[`missing_panics_doc`]: pickup expect method

close #10240

*Please write a short comment explaining your change (or "none" for internal only changes)*

changelog: [`missing_panics_doc`]: pickup expect method
This commit is contained in:
bors 2023-06-16 09:12:46 +00:00
commit 43ecf8ea7d
6 changed files with 115 additions and 3 deletions

View file

@ -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;

View file

@ -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");
@ -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) => {},

View file

@ -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>,

View file

@ -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)

View file

@ -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")
}
}

View file

@ -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