Skip to content

Commit 7147068

Browse files
committed
Auto merge of rust-lang#5590 - ebroto:issue_5579, r=phansch
Fix ICE caused in unwrap module changelog: Fix ICE in unwrap module with unexpected number of parameters for method of Option/Result Fixes rust-lang#5579
2 parents b20a9cd + 8d1029d commit 7147068

File tree

2 files changed

+27
-2
lines changed

2 files changed

+27
-2
lines changed

clippy_lints/src/unwrap.rs

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ use rustc_hir::{BinOpKind, Body, Expr, ExprKind, FnDecl, HirId, Path, QPath, UnO
88
use rustc_lint::{LateContext, LateLintPass};
99
use rustc_middle::hir::map::Map;
1010
use rustc_middle::lint::in_external_macro;
11+
use rustc_middle::ty::Ty;
1112
use rustc_session::{declare_lint_pass, declare_tool_lint};
1213
use rustc_span::source_map::Span;
1314

@@ -90,6 +91,14 @@ fn collect_unwrap_info<'a, 'tcx>(
9091
branch: &'tcx Expr<'_>,
9192
invert: bool,
9293
) -> Vec<UnwrapInfo<'tcx>> {
94+
fn is_relevant_option_call(cx: &LateContext<'_, '_>, ty: Ty<'_>, method_name: &str) -> bool {
95+
is_type_diagnostic_item(cx, ty, sym!(option_type)) && ["is_some", "is_none"].contains(&method_name)
96+
}
97+
98+
fn is_relevant_result_call(cx: &LateContext<'_, '_>, ty: Ty<'_>, method_name: &str) -> bool {
99+
is_type_diagnostic_item(cx, ty, sym!(result_type)) && ["is_ok", "is_err"].contains(&method_name)
100+
}
101+
93102
if let ExprKind::Binary(op, left, right) = &expr.kind {
94103
match (invert, op.node) {
95104
(false, BinOpKind::And) | (false, BinOpKind::BitAnd) | (true, BinOpKind::Or) | (true, BinOpKind::BitOr) => {
@@ -106,9 +115,8 @@ fn collect_unwrap_info<'a, 'tcx>(
106115
if let ExprKind::MethodCall(method_name, _, args) = &expr.kind;
107116
if let ExprKind::Path(QPath::Resolved(None, path)) = &args[0].kind;
108117
let ty = cx.tables.expr_ty(&args[0]);
109-
if is_type_diagnostic_item(cx, ty, sym!(option_type)) || is_type_diagnostic_item(cx, ty, sym!(result_type));
110118
let name = method_name.ident.as_str();
111-
if ["is_some", "is_none", "is_ok", "is_err"].contains(&&*name);
119+
if is_relevant_option_call(cx, ty, &name) || is_relevant_result_call(cx, ty, &name);
112120
then {
113121
assert!(args.len() == 1);
114122
let unwrappable = match name.as_ref() {

tests/ui/crashes/ice-5579.rs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
trait IsErr {
2+
fn is_err(&self, err: &str) -> bool;
3+
}
4+
5+
impl<T> IsErr for Option<T> {
6+
fn is_err(&self, _err: &str) -> bool {
7+
true
8+
}
9+
}
10+
11+
fn main() {
12+
let t = Some(1);
13+
14+
if t.is_err("") {
15+
t.unwrap();
16+
}
17+
}

0 commit comments

Comments
 (0)