Add off-by-default rule for unused call results#3906
Open
nitishagar wants to merge 1 commit into
Open
Conversation
38bfcf8 to
ca5efe3
Compare
Introduce an `unused-call-result` diagnostic that fires when a bare expression statement is a function/method call whose result is discarded and the result type is informative (not `None`, `Any`, or `Never`). The check sits in `binding_to_type_stmt_expr` as an `else if` branch after the existing `unused-coroutine` check, so the two diagnostics are mutually exclusive: a discarded coroutine call still emits only `unused-coroutine`. The guard `matches!(e, Expr::Call(_))` ensures assignments, bare names, comparisons, and similar non-call statements are not flagged. Results of type `Any` or that extend `Any` are also exempted, consistent with the coroutine rule. The rule defaults to `Severity::Ignore` and must be explicitly enabled. A new test file covers every spec boundary: the golden-path discard, assigned/None/Any/Never/extends-Any exemptions, method and constructor calls, non-call bare statements, the R5 coroutine mutual-exclusion case (rule ON + coroutine discard → exactly one `unused-coroutine` error), and the default-off regression. Fixes facebook#3719 Test Plan: - cargo test -p pyrefly_config test_doc_headers test_doc_severities - cargo test -p pyrefly --lib unused_call_result - cargo test -p pyrefly --lib simple::test_unused_coroutine - python3 test.py --no-test --no-conformance --no-jsonschema
ca5efe3 to
c141411
Compare
|
According to mypy_primer, this change doesn't affect type check results on a corpus of open source code. ✅ |
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Introduce an
unused-call-resultdiagnostic that fires when a bareexpression statement is a function/method call whose result is discarded
and the result type is informative (not
None,Any, orNever).The check sits in
binding_to_type_stmt_expras anelse ifbranchafter the existing
unused-coroutinecheck, so the two diagnosticsare mutually exclusive: a discarded coroutine call still emits only
unused-coroutine. The guardmatches!(e, Expr::Call(_))ensuresassignments, bare names, comparisons, and similar non-call statements
are not flagged. Results of type
Anyor that extendAnyare alsoexempted, consistent with the coroutine rule. The rule defaults to
Severity::Ignoreand must be explicitly enabled.A new test file covers every spec boundary: the golden-path discard,
assigned/None/Any/Never/extends-Any exemptions, method and constructor
calls, non-call bare statements, the R5 coroutine mutual-exclusion case
(rule ON + coroutine discard → exactly one
unused-coroutineerror),and the default-off regression.
Fixes #3719
Test Plan: