- 
                Notifications
    You must be signed in to change notification settings 
- Fork 13.9k
          suggest Option::as_deref(_mut) on type mismatch in option combinator if it passes typeck
          #111659
        
          New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
          
     Merged
      
      
    
  
     Merged
                    Changes from all commits
      Commits
    
    
            Show all changes
          
          
            2 commits
          
        
        Select commit
          Hold shift + click to select a range
      
      
    File filter
Filter by extension
Conversations
          Failed to load comments.   
        
        
          
      Loading
        
  Jump to
        
          Jump to file
        
      
      
          Failed to load files.   
        
        
          
      Loading
        
  Diff view
Diff view
There are no files selected for viewing
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              
        
          
          
            40 changes: 40 additions & 0 deletions
          
          40 
        
  tests/ui/mismatched_types/suggest-option-asderef-unfixable.rs
  
  
      
      
   
        
      
      
    
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              | Original file line number | Diff line number | Diff line change | 
|---|---|---|
| @@ -0,0 +1,40 @@ | ||
| fn produces_string() -> Option<String> { | ||
| Some("my cool string".to_owned()) | ||
| } | ||
|  | ||
| fn takes_str_but_too_many_refs(_: &&str) -> Option<()> { | ||
| Some(()) | ||
| } | ||
|  | ||
| fn no_args() -> Option<()> { | ||
| Some(()) | ||
| } | ||
|  | ||
| fn generic_ref<T>(_: &T) -> Option<()> { | ||
| Some(()) | ||
| } | ||
|  | ||
| extern "C" fn takes_str_but_wrong_abi(_: &str) -> Option<()> { | ||
| Some(()) | ||
| } | ||
|  | ||
| unsafe fn takes_str_but_unsafe(_: &str) -> Option<()> { | ||
| Some(()) | ||
| } | ||
|  | ||
| struct TypeWithoutDeref; | ||
|  | ||
| fn main() { | ||
| let _ = produces_string().and_then(takes_str_but_too_many_refs); | ||
| //~^ ERROR type mismatch in function arguments | ||
| let _ = produces_string().and_then(takes_str_but_wrong_abi); | ||
| //~^ ERROR expected a `FnOnce<(String,)>` closure, found `for<'a> extern "C" fn(&'a str) -> Option<()> {takes_str_but_wrong_abi}` | ||
| let _ = produces_string().and_then(takes_str_but_unsafe); | ||
| //~^ ERROR expected a `FnOnce<(String,)>` closure, found `for<'a> unsafe fn(&'a str) -> Option<()> {takes_str_but_unsafe}` | ||
| let _ = produces_string().and_then(no_args); | ||
| //~^ ERROR function is expected to take 1 argument, but it takes 0 arguments | ||
| let _ = produces_string().and_then(generic_ref); | ||
| //~^ ERROR type mismatch in function arguments | ||
| let _ = Some(TypeWithoutDeref).and_then(takes_str_but_too_many_refs); | ||
| //~^ ERROR type mismatch in function arguments | ||
| } | ||
        
          
          
            96 changes: 96 additions & 0 deletions
          
          96 
        
  tests/ui/mismatched_types/suggest-option-asderef-unfixable.stderr
  
  
      
      
   
        
      
      
    
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              | Original file line number | Diff line number | Diff line change | 
|---|---|---|
| @@ -0,0 +1,96 @@ | ||
| error[E0631]: type mismatch in function arguments | ||
| --> $DIR/suggest-option-asderef-unfixable.rs:28:40 | ||
| | | ||
| LL | fn takes_str_but_too_many_refs(_: &&str) -> Option<()> { | ||
| | ------------------------------------------------------ found signature defined here | ||
| ... | ||
| LL | let _ = produces_string().and_then(takes_str_but_too_many_refs); | ||
| | -------- ^^^^^^^^^^^^^^^^^^^^^^^^^^^ expected due to this | ||
| | | | ||
| | required by a bound introduced by this call | ||
| | | ||
| = note: expected function signature `fn(String) -> _` | ||
| found function signature `for<'a, 'b> fn(&'a &'b str) -> _` | ||
| note: required by a bound in `Option::<T>::and_then` | ||
| --> $SRC_DIR/core/src/option.rs:LL:COL | ||
|  | ||
| error[E0277]: expected a `FnOnce<(String,)>` closure, found `for<'a> extern "C" fn(&'a str) -> Option<()> {takes_str_but_wrong_abi}` | ||
| --> $DIR/suggest-option-asderef-unfixable.rs:30:40 | ||
| | | ||
| LL | let _ = produces_string().and_then(takes_str_but_wrong_abi); | ||
| | -------- ^^^^^^^^^^^^^^^^^^^^^^^ expected an `FnOnce<(String,)>` closure, found `for<'a> extern "C" fn(&'a str) -> Option<()> {takes_str_but_wrong_abi}` | ||
| | | | ||
| | required by a bound introduced by this call | ||
| | | ||
| = help: the trait `FnOnce<(String,)>` is not implemented for fn item `for<'a> extern "C" fn(&'a str) -> Option<()> {takes_str_but_wrong_abi}` | ||
| note: required by a bound in `Option::<T>::and_then` | ||
| --> $SRC_DIR/core/src/option.rs:LL:COL | ||
|  | ||
| error[E0277]: expected a `FnOnce<(String,)>` closure, found `for<'a> unsafe fn(&'a str) -> Option<()> {takes_str_but_unsafe}` | ||
| --> $DIR/suggest-option-asderef-unfixable.rs:32:40 | ||
| | | ||
| LL | let _ = produces_string().and_then(takes_str_but_unsafe); | ||
| | -------- ^^^^^^^^^^^^^^^^^^^^ call the function in a closure: `|| unsafe { /* code */ }` | ||
| | | | ||
| | required by a bound introduced by this call | ||
| | | ||
| = help: the trait `FnOnce<(String,)>` is not implemented for fn item `for<'a> unsafe fn(&'a str) -> Option<()> {takes_str_but_unsafe}` | ||
| = note: unsafe function cannot be called generically without an unsafe block | ||
| note: required by a bound in `Option::<T>::and_then` | ||
| --> $SRC_DIR/core/src/option.rs:LL:COL | ||
|  | ||
| error[E0593]: function is expected to take 1 argument, but it takes 0 arguments | ||
| --> $DIR/suggest-option-asderef-unfixable.rs:34:40 | ||
| | | ||
| LL | fn no_args() -> Option<()> { | ||
| | -------------------------- takes 0 arguments | ||
| ... | ||
| LL | let _ = produces_string().and_then(no_args); | ||
| | -------- ^^^^^^^ expected function that takes 1 argument | ||
| | | | ||
| | required by a bound introduced by this call | ||
| | | ||
| note: required by a bound in `Option::<T>::and_then` | ||
| --> $SRC_DIR/core/src/option.rs:LL:COL | ||
|  | ||
| error[E0631]: type mismatch in function arguments | ||
| --> $DIR/suggest-option-asderef-unfixable.rs:36:40 | ||
| | | ||
| LL | fn generic_ref<T>(_: &T) -> Option<()> { | ||
| | -------------------------------------- found signature defined here | ||
| ... | ||
| LL | let _ = produces_string().and_then(generic_ref); | ||
| | -------- ^^^^^^^^^^^ expected due to this | ||
| | | | ||
| | required by a bound introduced by this call | ||
| | | ||
| = note: expected function signature `fn(String) -> _` | ||
| found function signature `for<'a> fn(&'a _) -> _` | ||
| note: required by a bound in `Option::<T>::and_then` | ||
| --> $SRC_DIR/core/src/option.rs:LL:COL | ||
| help: do not borrow the argument | ||
| | | ||
| LL - fn generic_ref<T>(_: &T) -> Option<()> { | ||
| LL + fn generic_ref<T>(_: T) -> Option<()> { | ||
| | | ||
|  | ||
| error[E0631]: type mismatch in function arguments | ||
| --> $DIR/suggest-option-asderef-unfixable.rs:38:45 | ||
| | | ||
| LL | fn takes_str_but_too_many_refs(_: &&str) -> Option<()> { | ||
| | ------------------------------------------------------ found signature defined here | ||
| ... | ||
| LL | let _ = Some(TypeWithoutDeref).and_then(takes_str_but_too_many_refs); | ||
| | -------- ^^^^^^^^^^^^^^^^^^^^^^^^^^^ expected due to this | ||
| | | | ||
| | required by a bound introduced by this call | ||
| | | ||
| = note: expected function signature `fn(TypeWithoutDeref) -> _` | ||
| found function signature `for<'a, 'b> fn(&'a &'b str) -> _` | ||
| note: required by a bound in `Option::<T>::and_then` | ||
| --> $SRC_DIR/core/src/option.rs:LL:COL | ||
|  | ||
| error: aborting due to 6 previous errors | ||
|  | ||
| Some errors have detailed explanations: E0277, E0593, E0631. | ||
| For more information about an error, try `rustc --explain E0277`. | 
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              | Original file line number | Diff line number | Diff line change | 
|---|---|---|
| @@ -0,0 +1,30 @@ | ||
| // run-rustfix | ||
|  | ||
| 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 main() { | ||
| let _: Option<()> = produces_string().as_deref().and_then(takes_str); | ||
| //~^ ERROR type mismatch in function arguments | ||
| //~| HELP call `Option::as_deref()` first | ||
| let _: Option<Option<()>> = produces_string().as_deref().map(takes_str); | ||
| //~^ ERROR type mismatch in function arguments | ||
| //~| HELP call `Option::as_deref()` first | ||
| let _: Option<Option<()>> = produces_string().as_deref_mut().map(takes_str_mut); | ||
| //~^ ERROR type mismatch in function arguments | ||
| //~| HELP call `Option::as_deref_mut()` first | ||
| let _ = produces_string().and_then(generic); | ||
| } | 
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              | Original file line number | Diff line number | Diff line change | 
|---|---|---|
| @@ -0,0 +1,30 @@ | ||
| // run-rustfix | ||
|  | ||
| 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 main() { | ||
| let _: Option<()> = produces_string().and_then(takes_str); | ||
| //~^ ERROR type mismatch in function arguments | ||
| //~| HELP call `Option::as_deref()` first | ||
| let _: Option<Option<()>> = produces_string().map(takes_str); | ||
| //~^ ERROR type mismatch in function arguments | ||
| //~| HELP call `Option::as_deref()` first | ||
| let _: Option<Option<()>> = produces_string().map(takes_str_mut); | ||
| //~^ ERROR type mismatch in function arguments | ||
| //~| HELP call `Option::as_deref_mut()` first | ||
| let _ = produces_string().and_then(generic); | ||
| } | 
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              | Original file line number | Diff line number | Diff line change | 
|---|---|---|
| @@ -0,0 +1,63 @@ | ||
| error[E0631]: type mismatch in function arguments | ||
| --> $DIR/suggest-option-asderef.rs:20:52 | ||
| | | ||
| LL | fn takes_str(_: &str) -> Option<()> { | ||
| | ----------------------------------- found signature defined here | ||
| ... | ||
| LL | let _: Option<()> = produces_string().and_then(takes_str); | ||
| | -------- ^^^^^^^^^ expected due to this | ||
| | | | ||
| | required by a bound introduced by this call | ||
| | | ||
| = note: expected function signature `fn(String) -> _` | ||
| found function signature `for<'a> fn(&'a str) -> _` | ||
| note: required by a bound in `Option::<T>::and_then` | ||
| --> $SRC_DIR/core/src/option.rs:LL:COL | ||
| help: call `Option::as_deref()` first | ||
| | | ||
| LL | let _: Option<()> = produces_string().as_deref().and_then(takes_str); | ||
| | +++++++++++ | ||
|  | ||
| error[E0631]: type mismatch in function arguments | ||
| --> $DIR/suggest-option-asderef.rs:23:55 | ||
| | | ||
| LL | fn takes_str(_: &str) -> Option<()> { | ||
| | ----------------------------------- found signature defined here | ||
| ... | ||
| LL | let _: Option<Option<()>> = produces_string().map(takes_str); | ||
| | --- ^^^^^^^^^ expected due to this | ||
| | | | ||
| | required by a bound introduced by this call | ||
| | | ||
| = note: expected function signature `fn(String) -> _` | ||
| found function signature `for<'a> fn(&'a str) -> _` | ||
| note: required by a bound in `Option::<T>::map` | ||
| --> $SRC_DIR/core/src/option.rs:LL:COL | ||
| help: call `Option::as_deref()` first | ||
| | | ||
| LL | let _: Option<Option<()>> = produces_string().as_deref().map(takes_str); | ||
| | +++++++++++ | ||
|  | ||
| error[E0631]: type mismatch in function arguments | ||
| --> $DIR/suggest-option-asderef.rs:26:55 | ||
| | | ||
| LL | fn takes_str_mut(_: &mut str) -> Option<()> { | ||
| | ------------------------------------------- found signature defined here | ||
| ... | ||
| LL | let _: Option<Option<()>> = produces_string().map(takes_str_mut); | ||
| | --- ^^^^^^^^^^^^^ expected due to this | ||
| | | | ||
| | required by a bound introduced by this call | ||
| | | ||
| = note: expected function signature `fn(String) -> _` | ||
| found function signature `for<'a> fn(&'a mut str) -> _` | ||
| note: required by a bound in `Option::<T>::map` | ||
| --> $SRC_DIR/core/src/option.rs:LL:COL | ||
| help: call `Option::as_deref_mut()` first | ||
| | | ||
| LL | let _: Option<Option<()>> = produces_string().as_deref_mut().map(takes_str_mut); | ||
| | +++++++++++++++ | ||
|  | ||
| error: aborting due to 3 previous errors | ||
|  | ||
| For more information about this error, try `rustc --explain E0631`. | 
  Add this suggestion to a batch that can be applied as a single commit.
  This suggestion is invalid because no changes were made to the code.
  Suggestions cannot be applied while the pull request is closed.
  Suggestions cannot be applied while viewing a subset of changes.
  Only one suggestion per line can be applied in a batch.
  Add this suggestion to a batch that can be applied as a single commit.
  Applying suggestions on deleted lines is not supported.
  You must change the existing code in this line in order to create a valid suggestion.
  Outdated suggestions cannot be applied.
  This suggestion has been applied or marked resolved.
  Suggestions cannot be applied from pending reviews.
  Suggestions cannot be applied on multi-line comments.
  Suggestions cannot be applied while the pull request is queued to merge.
  Suggestion cannot be applied right now. Please check back later.
  
    
  
    
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should this case suggest
as_ref?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I initially had it suggest
as_ref()where applicable too, but there's already a help message for callingand_thenwith anfn(&T)on someOption<T>, though it suggests removing the borrow on the function parameter and I'm not sure how it would work out when there are two suggestions made by the compiler (is that fine?)