Skip to content

Commit 01f3d25

Browse files
Rollup merge of rust-lang#158467 - chenyukang:yukang-rework-on-fix-unused_assignments-147648, r=adwinwhite
Add proc macro for unused assignments and corresponding test rust-lang#147648 now is fixed by change rust-lang#151556 Add a test case and close the issue Fixes rust-lang#147648
2 parents 42ab90a + 67f85db commit 01f3d25

2 files changed

Lines changed: 41 additions & 0 deletions

File tree

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
#![feature(proc_macro_quote)]
2+
3+
extern crate proc_macro;
4+
use proc_macro::*;
5+
6+
#[proc_macro_derive(UnusedAssign)]
7+
pub fn generate(ts: TokenStream) -> TokenStream {
8+
let mut ts = ts.into_iter();
9+
let _pub = ts.next();
10+
let _struct = ts.next();
11+
let name = ts.next().unwrap();
12+
let TokenTree::Group(fields) = ts.next().unwrap() else { panic!() };
13+
let mut fields = fields.stream().into_iter();
14+
let field = fields.next().unwrap();
15+
16+
quote! {
17+
impl Drop for $name {
18+
fn drop(&mut self) {
19+
let Self { $field } = self;
20+
}
21+
}
22+
}
23+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
//@ check-pass
2+
//@ proc-macro: aux_issue_147648.rs
3+
4+
#![deny(unused_assignments)]
5+
#![allow(dead_code)]
6+
#![allow(unused_variables)]
7+
8+
extern crate aux_issue_147648;
9+
use aux_issue_147648::UnusedAssign;
10+
11+
#[derive(UnusedAssign)]
12+
pub struct MyError {
13+
source_code: (),
14+
}
15+
16+
fn main() {
17+
let _error = MyError { source_code: () };
18+
}

0 commit comments

Comments
 (0)