Skip to content

Commit 5d55533

Browse files
committed
syntax: Use let _ in #[derive(Debug)]
This should help avoid triggering the unused_results lint which can frequently be turned on. Closes #29710
1 parent 00aa3cb commit 5d55533

File tree

2 files changed

+41
-4
lines changed

2 files changed

+41
-4
lines changed

src/libsyntax/ext/deriving/debug.rs

+21-4
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@
99
// except according to those terms.
1010

1111
use ast;
12-
use ast::{MetaItem, Expr,};
13-
use codemap::Span;
12+
use ast::{MetaItem, Expr};
13+
use codemap::{Span, respan};
1414
use ext::base::{ExtCtxt, Annotatable};
1515
use ext::build::AstBuilder;
1616
use ext::deriving::generic::*;
@@ -97,7 +97,10 @@ fn show_substructure(cx: &mut ExtCtxt, span: Span,
9797
builder_expr.clone(),
9898
token::str_to_ident("field"),
9999
vec![field]);
100-
stmts.push(cx.stmt_expr(expr));
100+
101+
// Use `let _ = expr;` to avoid triggering the
102+
// unused_results lint.
103+
stmts.push(stmt_let_undescore(cx, span, expr));
101104
}
102105
} else {
103106
// normal struct/struct variant
@@ -119,7 +122,7 @@ fn show_substructure(cx: &mut ExtCtxt, span: Span,
119122
builder_expr.clone(),
120123
token::str_to_ident("field"),
121124
vec![name, field]);
122-
stmts.push(cx.stmt_expr(expr));
125+
stmts.push(stmt_let_undescore(cx, span, expr));
123126
}
124127
}
125128
stmts
@@ -135,3 +138,17 @@ fn show_substructure(cx: &mut ExtCtxt, span: Span,
135138
let block = cx.block(span, stmts, Some(expr));
136139
cx.expr_block(block)
137140
}
141+
142+
fn stmt_let_undescore(cx: &mut ExtCtxt,
143+
sp: Span,
144+
expr: P<ast::Expr>) -> P<ast::Stmt> {
145+
let local = P(ast::Local {
146+
pat: cx.pat_wild(sp),
147+
ty: None,
148+
init: Some(expr),
149+
id: ast::DUMMY_NODE_ID,
150+
span: sp,
151+
});
152+
let decl = respan(sp, ast::DeclLocal(local));
153+
P(respan(sp, ast::StmtDecl(P(decl), ast::DUMMY_NODE_ID)))
154+
}

src/test/run-pass/issue-29710.rs

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
// Copyright 2015 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
#![deny(unused_results)]
12+
#![allow(dead_code)]
13+
14+
#[derive(Debug)]
15+
struct A(usize);
16+
17+
#[derive(Debug)]
18+
struct B { a: usize }
19+
20+
fn main() {}

0 commit comments

Comments
 (0)