Skip to content
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

Handle attempts to have multiple cfgd tail expressions #117988

Merged
merged 1 commit into from
Nov 20, 2023

Commits on Nov 16, 2023

  1. Handle attempts to have multiple cfgd tail expressions

    When encountering code that seems like it might be trying to have
    multiple tail expressions depending on `cfg` information, suggest
    alternatives that will success to parse.
    
    ```rust
    fn foo() -> String {
        #[cfg(feature = "validation")]
        [1, 2, 3].iter().map(|c| c.to_string()).collect::<String>()
        #[cfg(not(feature = "validation"))]
        String::new()
    }
    ```
    
    ```
    error: expected `;`, found `#`
      --> $DIR/multiple-tail-expr-behind-cfg.rs:5:64
       |
    LL |     #[cfg(feature = "validation")]
       |     ------------------------------ only `;` terminated statements or tail expressions are allowed after this attribute
    LL |     [1, 2, 3].iter().map(|c| c.to_string()).collect::<String>()
       |                                                                ^ expected `;` here
    LL |     #[cfg(not(feature = "validation"))]
       |     - unexpected token
       |
    help: add `;` here
       |
    LL |     [1, 2, 3].iter().map(|c| c.to_string()).collect::<String>();
       |                                                                +
    help: alternatively, consider surrounding the expression with a block
       |
    LL |     { [1, 2, 3].iter().map(|c| c.to_string()).collect::<String>() }
       |     +                                                             +
    help: it seems like you are trying to provide different expressions depending on `cfg`, consider using `if cfg!(..)`
       |
    LL ~     if cfg!(feature = "validation") {
    LL ~         [1, 2, 3].iter().map(|c| c.to_string()).collect::<String>()
    LL ~     } else if cfg!(not(feature = "validation")) {
    LL ~         String::new()
    LL +     }
       |
    ```
    
    Fix rust-lang#106020.
    estebank committed Nov 16, 2023
    Configuration menu
    Copy the full SHA
    a16722d View commit details
    Browse the repository at this point in the history