Skip to content

Commit 9178eb4

Browse files
committed
Handle trait objects
1 parent 1228868 commit 9178eb4

File tree

1 file changed

+24
-6
lines changed

1 file changed

+24
-6
lines changed

src/librustc_lint/unused.rs

+24-6
Original file line numberDiff line numberDiff line change
@@ -65,13 +65,25 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for UnusedResults {
6565
true
6666
} else {
6767
match t.sty {
68-
ty::Adt(def, _) => check_must_use(cx, def.did, s.span, ""),
68+
ty::Adt(def, _) => check_must_use(cx, def.did, s.span, "", ""),
6969
ty::Opaque(def, _) => {
7070
let mut must_use = false;
7171
for (predicate, _) in cx.tcx.predicates_of(def).predicates {
7272
if let ty::Predicate::Trait(ref poly_trait_predicate) = predicate {
7373
let trait_ref = poly_trait_predicate.skip_binder().trait_ref;
74-
if check_must_use(cx, trait_ref.def_id, s.span, "implementer of ") {
74+
if check_must_use(cx, trait_ref.def_id, s.span, "implementer of ", "") {
75+
must_use = true;
76+
break;
77+
}
78+
}
79+
}
80+
must_use
81+
}
82+
ty::Dynamic(binder, _) => {
83+
let mut must_use = false;
84+
for predicate in binder.skip_binder().iter() {
85+
if let ty::ExistentialPredicate::Trait(ref trait_ref) = predicate {
86+
if check_must_use(cx, trait_ref.def_id, s.span, "", " trait object") {
7587
must_use = true;
7688
break;
7789
}
@@ -107,7 +119,7 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for UnusedResults {
107119
};
108120
if let Some(def) = maybe_def {
109121
let def_id = def.def_id();
110-
fn_warned = check_must_use(cx, def_id, s.span, "return value of ");
122+
fn_warned = check_must_use(cx, def_id, s.span, "return value of ", "");
111123
} else if type_permits_lack_of_use {
112124
// We don't warn about unused unit or uninhabited types.
113125
// (See https://github.com/rust-lang/rust/issues/43806 for details.)
@@ -161,11 +173,17 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for UnusedResults {
161173
cx.span_lint(UNUSED_RESULTS, s.span, "unused result");
162174
}
163175

164-
fn check_must_use(cx: &LateContext, def_id: DefId, sp: Span, describe_path: &str) -> bool {
176+
fn check_must_use(
177+
cx: &LateContext,
178+
def_id: DefId,
179+
sp: Span,
180+
descr_pre_path: &str,
181+
descr_post_path: &str,
182+
) -> bool {
165183
for attr in cx.tcx.get_attrs(def_id).iter() {
166184
if attr.check_name("must_use") {
167-
let msg = format!("unused {}`{}` that must be used",
168-
describe_path, cx.tcx.item_path_str(def_id));
185+
let msg = format!("unused {}`{}`{} that must be used",
186+
descr_pre_path, descr_post_path, cx.tcx.item_path_str(def_id));
169187
let mut err = cx.struct_span_lint(UNUSED_MUST_USE, sp, &msg);
170188
// check for #[must_use = "..."]
171189
if let Some(note) = attr.value_str() {

0 commit comments

Comments
 (0)