File: suggest-option-asderef.rs

package info (click to toggle)
rustc 1.85.0%2Bdfsg3-1
  • links: PTS, VCS
  • area: main
  • in suites: experimental, sid, trixie
  • size: 893,396 kB
  • sloc: xml: 158,127; python: 35,830; javascript: 19,497; cpp: 19,002; sh: 17,245; ansic: 13,127; asm: 4,376; makefile: 1,051; perl: 29; lisp: 29; ruby: 19; sql: 11
file content (43 lines) | stat: -rw-r--r-- 1,404 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
// this isn't auto-fixable now because we produce two similar suggestions

fn produces_string() -> Option<String> {
    Some("my cool string".to_owned())
}

fn takes_str(_: &str) -> Option<()> {
    Some(())
}

fn takes_str_mut(_: &mut str) -> Option<()> {
    Some(())
}

fn generic<T>(_: T) -> Option<()> {
    Some(())
}

fn generic_ref<T>(_: &T) -> Option<()> {
    //~^ HELP consider adjusting the signature so it does not borrow its argument
    Some(())
}

fn main() {
    let _: Option<()> = produces_string().and_then(takes_str);
    //~^ ERROR type mismatch in function arguments
    //~| HELP call `Option::as_deref()` first
    //~| HELP consider wrapping the function in a closure
    let _: Option<Option<()>> = produces_string().map(takes_str);
    //~^ ERROR type mismatch in function arguments
    //~| HELP call `Option::as_deref()` first
    //~| HELP consider wrapping the function in a closure
    let _: Option<Option<()>> = produces_string().map(takes_str_mut);
    //~^ ERROR type mismatch in function arguments
    //~| HELP call `Option::as_deref_mut()` first
    //~| HELP consider wrapping the function in a closure
    let _ = produces_string().and_then(generic);

    let _ = produces_string().and_then(generic_ref);
    //~^ ERROR type mismatch in function arguments
    //~| HELP call `Option::as_deref()` first
    //~| HELP consider wrapping the function in a closure
}