missing_panics_doc: pickup expect method

This commit is contained in:
Kisaragi Marine 2023-06-14 22:07:31 +09:00
parent 1d0d686f10
commit 79f93a655a
No known key found for this signature in database
GPG Key ID: 62B80306B822AE9E
2 changed files with 33 additions and 2 deletions

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

@ -0,0 +1,31 @@
#![warn(clippy::missing_panics_doc)]
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")
}
fn main() {}