3 #![allow(unused_variables, clippy::unnecessary_wraps)]
5 fn option_unwrap_or() {
10 Some(1).unwrap_or(42);
13 Some(1).unwrap_or(1 + 42);
24 Some("Bob").unwrap_or("Alice");
46 // cases where the none arm isn't a constant expression
47 // are not linted due to potential ownership issues
49 // ownership issue example, don't lint
51 let mut option: Option<NonCopyable> = None;
55 option = Some(NonCopyable);
61 // ownership issue example, don't lint
62 let option: Option<&str> = None;
65 None => &format!("{} {}!", "hello", "world"),
69 fn result_unwrap_or() {
71 Ok::<i32, &str>(1).unwrap_or(42);
73 // int case, scrutinee is a binding
74 let a = Ok::<i32, &str>(1);
77 // int case, suggestion must surround Result expr with parenthesis
78 (Ok(1) as Result<i32, &str>).unwrap_or(42);
80 // method call case, suggestion must not surround Result expr `s.method()` with parenthesis
83 fn method(self) -> Option<i32> {
88 s.method().unwrap_or(42);
91 Ok::<i32, &str>(1).unwrap_or(42);
94 Ok::<i32, &str>(1).unwrap_or(1 + 42);
98 Ok::<i32, &str>(1).unwrap_or({
105 Ok::<&str, &str>("Bob").unwrap_or("Alice");
108 match Ok::<i32, &str>(1) {
112 match Ok::<i32, &str>(1) {
117 match Ok::<i32, &str>(j) {
121 match Ok::<i32, &str>(j) {
127 // don't lint, Err value is used
128 match Ok::<&str, &str>("Alice") {
132 // could lint, but unused_variables takes care of it
133 match Ok::<&str, &str>("Alice") {
139 // don't lint in const fn
140 const fn const_fn_option_unwrap_or() {
147 const fn const_fn_result_unwrap_or() {
148 match Ok::<&str, &str>("Alice") {
155 macro_rules! some_macro {
157 if 1 > 2 { Some(1) } else { None }
162 let _ = some_macro!().unwrap_or(0);
167 fn format_name(name: Option<&Rc<str>>) -> &str {
174 fn implicit_deref_ref() {
175 let _: &str = match Some(&"bye") {