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

Invalid note: "the function expects a value to always be returned, but loops might run zero times" #122561

Closed
GuillaumeGomez opened this issue Mar 15, 2024 · 3 comments · Fixed by #123125
Assignees
Labels
A-diagnostics Area: Messages for errors, warnings, and lints D-confusing Diagnostics: Confusing error or lint that should be reworked. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@GuillaumeGomez
Copy link
Member

GuillaumeGomez commented Mar 15, 2024

Code

fn is_function_block(cx: &LateContext<'_>, mut expr_hir_id: HirId) -> bool {
    for i in 0.. {
        // We get the parent block.
        if let Some(scope_id) = cx.tcx.hir().get_enclosing_scope(expr_hir_id)
            && let Node::Block(block) = cx.tcx.hir().hir_node(scope_id)
            // We check that there is no statements in the block.
            && block.stmts.is_empty()
            // We check that the return statements is the current expression.
            && let Some(expr) = block.expr
            && expr_hir_id == expr.hir_id
        {
            expr_hir_id = expr.hir_id;
        } else {
            return i != 0;
        }
    }
}

Current output

32 |   fn is_function_block(cx: &LateContext<'_>, mut expr_hir_id: HirId) -> bool {
   |                                                                         ---- expected `bool` because of return type
33 | /     for i in 0.. {
34 | |         // We get the parent block.
35 | |         if let Some(scope_id) = cx.tcx.hir().get_enclosing_scope(expr_hir_id)
36 | |             && let Node::Block(block) = cx.tcx.hir().hir_node(scope_id)
...  |
46 | |         }
47 | |     }
   | |_____^ expected `bool`, found `()`
   |
note: the function expects a value to always be returned, but loops might run zero times
  --> clippy_lints/src/transmute/missing_transmute_annotations.rs:33:5
   |
33 |     for i in 0.. {
   |     ^^^^^^^^^^^^ this might have zero elements to iterate on
...
45 |             return i != 0;
   |             ------------- if the loop doesn't execute, this value would never get returned
help: return a value for the case when the loop has zero elements to iterate on
   |
47 ~     }
48 +     /* `bool` value */
   |
help: otherwise consider changing the return type to account for that possibility
   |
32 ~ fn is_function_block(cx: &LateContext<'_>, mut expr_hir_id: HirId) -> Option<bool> {
33 |     for i in 0.. {
 ...
44 |         } else {
45 ~             return Some(i != 0);
46 |         }
47 ~     }
48 +     None

Desired output

It should not emit a warning in this case since the range can never not iterate

Rationale and extra context

No response

Other cases

No response

Rust Version

rustc 1.78.0-nightly (7d3702e47 2024-03-06)
binary: rustc
commit-hash: 7d3702e472b99be0f5de6608dd87af1df8f99428
commit-date: 2024-03-06
host: x86_64-unknown-linux-gnu
release: 1.78.0-nightly
LLVM version: 18.1.0

Anything else?

No response

@GuillaumeGomez GuillaumeGomez added A-diagnostics Area: Messages for errors, warnings, and lints T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Mar 15, 2024
@jieyouxu jieyouxu added the D-confusing Diagnostics: Confusing error or lint that should be reworked. label Mar 15, 2024
@gurry
Copy link
Contributor

gurry commented Mar 16, 2024

@rustbot claim

@gurry
Copy link
Contributor

gurry commented Mar 16, 2024

Minimized:

fn test() -> bool {
    for i in 0.. {
        return false;
    }
}

@GuillaumeGomez
Copy link
Member Author

Thanks for the minimized version. Forgot to update the issue... ^^'

bors added a commit to rust-lang-ci/rust that referenced this issue Apr 19, 2024
…iters-2, r=estebank

Remove suggestion about iteration count in coerce

Fixes rust-lang#122561

The iteration count-centric suggestion was implemented in PR rust-lang#100094, but it was based on the wrong assumption that the type mismatch error depends on the number of times the loop iterates. As it turns out, that is not true (see this comment for details: rust-lang#122679 (comment))

This PR attempts to remedy the situation by changing the suggestion from the one centered on iteration count to a simple suggestion to add a return value.

It should also fix rust-lang#100285 by simply making it redundant.
@bors bors closed this as completed in 06e88c3 May 5, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-diagnostics Area: Messages for errors, warnings, and lints D-confusing Diagnostics: Confusing error or lint that should be reworked. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
3 participants