1| |#![allow(unused_assignments, unused_variables)] 2| | 3| 1|fn main() { 4| 1| // Initialize test constants in a way that cannot be determined at compile time, to ensure 5| 1| // rustc and LLVM cannot optimize out statements (or coverage counters) downstream from 6| 1| // dependent conditions. 7| 1| let is_true = std::env::args().len() == 1; 8| 1| let is_false = ! is_true; 9| 1| 10| 1| let mut some_string = Some(String::from("the string content")); 11| 1| println!( 12| 1| "The string or alt: {}" 13| 1| , 14| 1| some_string 15| 1| . 16| 1| unwrap_or_else 17| 1| ( 18| 1| || 19| 0| { 20| 0| let mut countdown = 0; 21| 0| if is_false { 22| 0| countdown = 10; 23| 0| } 24| 0| "alt string 1".to_owned() 25| 1| } 26| 1| ) 27| 1| ); 28| 1| 29| 1| some_string = Some(String::from("the string content")); 30| 1| let 31| 1| a 32| 1| = 33| 1| || 34| 0| { 35| 0| let mut countdown = 0; 36| 0| if is_false { 37| 0| countdown = 10; 38| 0| } 39| 0| "alt string 2".to_owned() 40| 1| }; 41| 1| println!( 42| 1| "The string or alt: {}" 43| 1| , 44| 1| some_string 45| 1| . 46| 1| unwrap_or_else 47| 1| ( 48| 1| a 49| 1| ) 50| 1| ); 51| 1| 52| 1| some_string = None; 53| 1| println!( 54| 1| "The string or alt: {}" 55| 1| , 56| 1| some_string 57| 1| . 58| 1| unwrap_or_else 59| 1| ( 60| 1| || 61| 1| { 62| 1| let mut countdown = 0; 63| 1| if is_false { 64| 0| countdown = 10; 65| 1| } 66| 1| "alt string 3".to_owned() 67| 1| } 68| 1| ) 69| 1| ); 70| 1| 71| 1| some_string = None; 72| 1| let 73| 1| a 74| 1| = 75| 1| || 76| 1| { 77| 1| let mut countdown = 0; 78| 1| if is_false { 79| 0| countdown = 10; 80| 1| } 81| 1| "alt string 4".to_owned() 82| 1| }; 83| 1| println!( 84| 1| "The string or alt: {}" 85| 1| , 86| 1| some_string 87| 1| . 88| 1| unwrap_or_else 89| 1| ( 90| 1| a 91| 1| ) 92| 1| ); 93| 1| 94| 1| let 95| 1| quote_closure 96| 1| = 97| 1| |val| 98| 5| { 99| 5| let mut countdown = 0; 100| 5| if is_false { 101| 0| countdown = 10; 102| 5| } 103| 5| format!("'{}'", val) 104| 5| }; 105| 1| println!( 106| 1| "Repeated, quoted string: {:?}" 107| 1| , 108| 1| std::iter::repeat("repeat me") 109| 1| .take(5) 110| 1| .map 111| 1| ( 112| 1| quote_closure 113| 1| ) 114| 1| .collect::>() 115| 1| ); 116| 1| 117| 1| let 118| 1| _unused_closure 119| 1| = 120| 1| | 121| | mut countdown 122| | | 123| 0| { 124| 0| if is_false { 125| 0| countdown = 10; 126| 0| } 127| 0| "closure should be unused".to_owned() 128| 1| }; 129| 1| 130| 1| let mut countdown = 10; 131| 1| let _short_unused_closure = | _unused_arg: u8 | countdown += 1; ^0 132| 1| 133| 1| // Macros can sometimes confuse the coverage results. Compare this next assignment, with an 134| 1| // unused closure that invokes the `println!()` macro, with the closure assignment above, that 135| 1| // does not use a macro. The closure above correctly shows `0` executions. 136| 1| let _short_unused_closure = | _unused_arg: u8 | println!("not called"); 137| 1| // The closure assignment above is executed, with a line count of `1`, but the `println!()` 138| 1| // could not have been called, and yet, there is no indication that it wasn't... 139| 1| 140| 1| // ...but adding block braces gives the expected result, showing the block was not executed. 141| 1| let _short_unused_closure_block = | _unused_arg: u8 | { println!("not called") }; ^0 142| 1| 143| 1| let _shortish_unused_closure = | _unused_arg: u8 | { 144| 0| println!("not called") 145| 1| }; 146| 1| 147| 1| let _as_short_unused_closure = | 148| | _unused_arg: u8 149| 1| | { println!("not called") }; ^0 150| 1| 151| 1| let _almost_as_short_unused_closure = | 152| | _unused_arg: u8 153| 1| | { println!("not called") } ^0 154| 1| ; 155| 1|}