Skip to content

Commit

Permalink
syntax: Use let _ in #[derive(Debug)]
Browse files Browse the repository at this point in the history
This should help avoid triggering the unused_results lint which can frequently
be turned on.

Closes #29710
  • Loading branch information
alexcrichton committed Nov 9, 2015
1 parent 00aa3cb commit 5d55533
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 4 deletions.
25 changes: 21 additions & 4 deletions src/libsyntax/ext/deriving/debug.rs
Expand Up @@ -9,8 +9,8 @@
// except according to those terms.

use ast;
use ast::{MetaItem, Expr,};
use codemap::Span;
use ast::{MetaItem, Expr};
use codemap::{Span, respan};
use ext::base::{ExtCtxt, Annotatable};
use ext::build::AstBuilder;
use ext::deriving::generic::*;
Expand Down Expand Up @@ -97,7 +97,10 @@ fn show_substructure(cx: &mut ExtCtxt, span: Span,
builder_expr.clone(),
token::str_to_ident("field"),
vec![field]);
stmts.push(cx.stmt_expr(expr));

// Use `let _ = expr;` to avoid triggering the
// unused_results lint.
stmts.push(stmt_let_undescore(cx, span, expr));
}
} else {
// normal struct/struct variant
Expand All @@ -119,7 +122,7 @@ fn show_substructure(cx: &mut ExtCtxt, span: Span,
builder_expr.clone(),
token::str_to_ident("field"),
vec![name, field]);
stmts.push(cx.stmt_expr(expr));
stmts.push(stmt_let_undescore(cx, span, expr));
}
}
stmts
Expand All @@ -135,3 +138,17 @@ fn show_substructure(cx: &mut ExtCtxt, span: Span,
let block = cx.block(span, stmts, Some(expr));
cx.expr_block(block)
}

fn stmt_let_undescore(cx: &mut ExtCtxt,
sp: Span,
expr: P<ast::Expr>) -> P<ast::Stmt> {
let local = P(ast::Local {
pat: cx.pat_wild(sp),
ty: None,
init: Some(expr),
id: ast::DUMMY_NODE_ID,
span: sp,
});
let decl = respan(sp, ast::DeclLocal(local));
P(respan(sp, ast::StmtDecl(P(decl), ast::DUMMY_NODE_ID)))
}
20 changes: 20 additions & 0 deletions src/test/run-pass/issue-29710.rs
@@ -0,0 +1,20 @@
// Copyright 2015 The Rust Project Developers. See the COPYRIGHT
// file at the top-level directory of this distribution and at
// http://rust-lang.org/COPYRIGHT.
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.

#![deny(unused_results)]
#![allow(dead_code)]

#[derive(Debug)]
struct A(usize);

#[derive(Debug)]
struct B { a: usize }

fn main() {}

0 comments on commit 5d55533

Please sign in to comment.