Skip to content

Commit c158a0f

Browse files
committed
Remove unnecessary unwraps
1 parent 33cde4a commit c158a0f

File tree

1 file changed

+34
-23
lines changed

1 file changed

+34
-23
lines changed

src/librustc_typeck/check/method/suggest.rs

Lines changed: 34 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -91,14 +91,21 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> {
9191
CandidateSource::ImplSource(impl_did) => {
9292
// Provide the best span we can. Use the item, if local to crate, else
9393
// the impl, if local to crate (item may be defaulted), else nothing.
94-
let item = self.associated_item(impl_did, item_name, Namespace::Value)
95-
.or_else(|| {
96-
self.associated_item(
97-
self.tcx.impl_trait_ref(impl_did).unwrap().def_id,
98-
item_name,
99-
Namespace::Value,
100-
)
101-
}).unwrap();
94+
let item = match self.associated_item(
95+
impl_did,
96+
item_name,
97+
Namespace::Value,
98+
).or_else(|| {
99+
let impl_trait_ref = self.tcx.impl_trait_ref(impl_did)?;
100+
self.associated_item(
101+
impl_trait_ref.def_id,
102+
item_name,
103+
Namespace::Value,
104+
)
105+
}) {
106+
Some(item) => item,
107+
None => continue,
108+
};
102109
let note_span = self.tcx.hir().span_if_local(item.def_id).or_else(|| {
103110
self.tcx.hir().span_if_local(impl_did)
104111
});
@@ -132,9 +139,14 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> {
132139
}
133140
}
134141
CandidateSource::TraitSource(trait_did) => {
135-
let item = self
136-
.associated_item(trait_did, item_name, Namespace::Value)
137-
.unwrap();
142+
let item = match self.associated_item(
143+
trait_did,
144+
item_name,
145+
Namespace::Value)
146+
{
147+
Some(item) => item,
148+
None => continue,
149+
};
138150
let item_span = self.tcx.sess.source_map()
139151
.def_span(self.tcx.def_span(item.def_id));
140152
if sources.len() > 1 {
@@ -251,8 +263,7 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> {
251263
if let &QPath::Resolved(_, ref path) = &qpath {
252264
if let hir::def::Res::Local(hir_id) = path.res {
253265
let span = tcx.hir().span_by_hir_id(hir_id);
254-
let snippet = tcx.sess.source_map().span_to_snippet(span)
255-
.unwrap();
266+
let snippet = tcx.sess.source_map().span_to_snippet(span);
256267
let filename = tcx.sess.source_map().span_to_filename(span);
257268

258269
let parent_node = self.tcx.hir().get_by_hir_id(
@@ -263,12 +274,12 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> {
263274
concrete_type,
264275
);
265276

266-
match (filename, parent_node) {
277+
match (filename, parent_node, snippet) {
267278
(FileName::Real(_), Node::Local(hir::Local {
268279
source: hir::LocalSource::Normal,
269280
ty,
270281
..
271-
})) => {
282+
}), Ok(ref snippet)) => {
272283
err.span_suggestion(
273284
// account for `let x: _ = 42;`
274285
// ^^^^
@@ -375,14 +386,14 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> {
375386
self.tcx.hir().get_parent_node_by_hir_id(expr.hir_id),
376387
);
377388

378-
let span = call_expr.span.trim_start(item_name.span).unwrap();
379-
380-
err.span_suggestion(
381-
span,
382-
"remove the arguments",
383-
String::new(),
384-
Applicability::MaybeIncorrect,
385-
);
389+
if let Some(span) = call_expr.span.trim_start(item_name.span) {
390+
err.span_suggestion(
391+
span,
392+
"remove the arguments",
393+
String::new(),
394+
Applicability::MaybeIncorrect,
395+
);
396+
}
386397
}
387398
}
388399

0 commit comments

Comments
 (0)