- 
                Notifications
    
You must be signed in to change notification settings  - Fork 13.9k
 
For [E0308]: mismatched types, when expr is in an arm's body, not add semicolon ';' at the end of it. #126455
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
      
      
    
      
        
          +170
        
        
          −9
        
        
          
        
      
    
  
  
     Merged
                    Changes from all commits
      Commits
    
    
  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
    
  
  
    
              
              
        
          
          
            34 changes: 34 additions & 0 deletions
          
          34 
        
  tests/ui/mismatched_types/mismatched-types-issue-126222.fixed
  
  
      
      
   
        
      
      
    
  
    
      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,34 @@ | ||
| //@ run-rustfix | ||
| #![allow(unreachable_code, dead_code)] | ||
| 
     | 
||
| fn main() { | ||
| fn mismatch_types1() -> i32 { | ||
| match 1 { | ||
| x => return dbg!(x), //~ ERROR mismatched types | ||
| } | ||
| todo!() | ||
| } | ||
| 
     | 
||
| fn mismatch_types2() -> i32 { | ||
| match 2 { | ||
| x => { | ||
| return dbg!(x) //~ ERROR mismatched types | ||
| } | ||
| } | ||
| todo!() | ||
| } | ||
| 
     | 
||
| fn mismatch_types3() -> i32 { | ||
| match 1 { | ||
| _ => return dbg!(1) //~ ERROR mismatched types | ||
| } | ||
| todo!() | ||
| } | ||
| 
     | 
||
| fn mismatch_types4() -> i32 { | ||
| match 1 { | ||
| _ => {return dbg!(1)} //~ ERROR mismatched types | ||
| } | ||
| todo!() | ||
| } | ||
| } | 
        
          
          
            34 changes: 34 additions & 0 deletions
          
          34 
        
  tests/ui/mismatched_types/mismatched-types-issue-126222.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,34 @@ | ||
| //@ run-rustfix | ||
| #![allow(unreachable_code, dead_code)] | ||
| 
     | 
||
| fn main() { | ||
| fn mismatch_types1() -> i32 { | ||
| match 1 { | ||
| x => dbg!(x), //~ ERROR mismatched types | ||
| } | ||
| todo!() | ||
| } | ||
| 
     | 
||
| fn mismatch_types2() -> i32 { | ||
| match 2 { | ||
| x => { | ||
| dbg!(x) //~ ERROR mismatched types | ||
| } | ||
| } | ||
| todo!() | ||
| } | ||
| 
     | 
||
| fn mismatch_types3() -> i32 { | ||
| match 1 { | ||
| _ => dbg!(1) //~ ERROR mismatched types | ||
| } | ||
| todo!() | ||
| } | ||
| 
     | 
||
| fn mismatch_types4() -> i32 { | ||
| match 1 { | ||
| _ => {dbg!(1)} //~ ERROR mismatched types | ||
| } | ||
| todo!() | ||
| } | ||
| } | 
        
          
          
            51 changes: 51 additions & 0 deletions
          
          51 
        
  tests/ui/mismatched_types/mismatched-types-issue-126222.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,51 @@ | ||
| error[E0308]: mismatched types | ||
| --> $DIR/mismatched-types-issue-126222.rs:7:18 | ||
| | | ||
| LL | x => dbg!(x), | ||
| | ^^^^^^^ expected `()`, found integer | ||
| | | ||
| = note: this error originates in the macro `dbg` (in Nightly builds, run with -Z macro-backtrace for more info) | ||
| help: you might have meant to return this value | ||
| | | ||
| LL | x => return dbg!(x), | ||
| | ++++++ | ||
| 
     | 
||
| error[E0308]: mismatched types | ||
| --> $DIR/mismatched-types-issue-126222.rs:15:17 | ||
| | | ||
| LL | dbg!(x) | ||
| | ^^^^^^^ expected `()`, found integer | ||
| | | ||
| = note: this error originates in the macro `dbg` (in Nightly builds, run with -Z macro-backtrace for more info) | ||
| help: you might have meant to return this value | ||
| | | ||
| LL | return dbg!(x) | ||
| | ++++++ | ||
| 
     | 
||
| error[E0308]: mismatched types | ||
| --> $DIR/mismatched-types-issue-126222.rs:23:18 | ||
| | | ||
| LL | _ => dbg!(1) | ||
| | ^^^^^^^ expected `()`, found integer | ||
| | | ||
| = note: this error originates in the macro `dbg` (in Nightly builds, run with -Z macro-backtrace for more info) | ||
| help: you might have meant to return this value | ||
| | | ||
| LL | _ => return dbg!(1) | ||
| | ++++++ | ||
| 
     | 
||
| error[E0308]: mismatched types | ||
| --> $DIR/mismatched-types-issue-126222.rs:30:19 | ||
| | | ||
| LL | _ => {dbg!(1)} | ||
| | ^^^^^^^ expected `()`, found integer | ||
| | | ||
| = note: this error originates in the macro `dbg` (in Nightly builds, run with -Z macro-backtrace for more info) | ||
| help: you might have meant to return this value | ||
| | | ||
| LL | _ => {return dbg!(1)} | ||
| | ++++++ | ||
| 
     | 
||
| error: aborting due to 4 previous errors | ||
| 
     | 
||
| For more information about this error, try `rustc --explain E0308`. | ||
  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.
I'm somewhat surprised: shouldn't these suggestions include a trailing
;?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'm assuming this is because
dbgexpands to a block expression, right?Uh oh!
There was an error while loading. Please reload this page.
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.
Thank you.
Here I think because in the return expression after match arm the "; " is unnecessary, and the diagnostic suggestion may make error expr like "return something; ,", so in this case, I recommend using the
return somethingwithout semicolon;.For me, it's not necessary to add ';' here whether it's expanded into a block expr or not. I'm not sure if I'm thinking about this fully. Please help me give an accurate judgement.
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.
It is idiomatic for statements like
returnto be ended with a;, regardless of whether it is valid code or not. That being said, it doesn't matter as much, as long as the suggestions produce valid code.