rust - How do I eliminate the spurious warning "value assigned is never read" in a macro? -


i new rust , learning write own macros. macro should fill struct matrixxf macro vec! vec<t>.

//fills matrix matlab syntax macro_rules! mat {     [ $($( $x: expr ),*);* ] => {{         let mut tmp_vec = vec::new();         let mut rows = 0;         let mut cols = 0;         let mut is_first_row_collected = false;         $(             let mut inner_cols = 0;             $(                 tmp_vec.push($x);                 inner_cols += 1;             )*             if is_first_row_collected {//if read first row can check other rows have same length                 assert!(inner_cols == cols);             } else {                 is_first_row_collected = true;                 cols = inner_cols;             }             rows += 1;         )*         matrixxf::construct(tmp_vec, rows, cols)//fills matrixxf fields     }} } 

and use way:

let mat = mat![1.0, 2.0, 3.0; 4.0, 5.0, 6.0]; 

everything ok, compiler shows me following warning:

7:23 warning: value assigned is_first_row_collected never read, #[warn(unused_assignments)] on default :7 is_first_row_collected = true ; cols = inner_cols ; } rows += 1 ; ) *

maybe misunderstood something, use is_first_row_collected when checking first row visited. possible rewrite code avoid warning?

the warning real; let's use modified example doesn't rely on structure didn't provide in question:

macro_rules! mat {     [ $($( $x: expr ),*);* ] => {{         let mut tmp_vec = vec::new();         let mut rows = 0;         let mut cols = 0;         let mut is_first_row_collected = false;         $(             let mut inner_cols = 0;             $(                 tmp_vec.push($x);                 inner_cols += 1;             )*             if is_first_row_collected {//if read first row can check other rows have same length                 assert!(inner_cols == cols);             } else {                 is_first_row_collected = true;                 cols = inner_cols;             }             rows += 1;         )*         (tmp_vec, rows, cols)     }} }  fn main() {     let _mat = mat![1.0, 2.0, 3.0; 4.0, 5.0, 6.0]; } 

we can use compiler see expanded version is:

rustc -z unstable-options --pretty expanded example.rs 

this big, ugly blob of code, i'll trim down relevant parts:

fn main() {     let mut is_first_row_collected = false;      if is_first_row_collected {         // removed     } else {         is_first_row_collected = true;     }      if is_first_row_collected {         // removed     } else {         is_first_row_collected = true;     } } 

so, indeed, value assigned never read. of course, human can see that particular flow shouldn't happen, , perhaps request enhancement compiler track that.

ideally, you'd rework macro not have underlying problem. francis gagné shows great way of doing that. if can't rework macro, can allow warning. unfortunately, don't know of way add #[allow(unused_assignments)] declaration on fn or mod, seems you'd have changes macro anyway.


Comments

Popular posts from this blog

1111. appearing after print sequence - php -

java - WARN : org.springframework.web.servlet.PageNotFound - No mapping found for HTTP request with URI [/board/] in DispatcherServlet with name 'appServlet' -

Ruby on Rails, ActiveRecord, Postgres, UTF-8 and ASCII-8BIT encodings -