Skip to content

fix(no-unnecessary-condition): use resolved call types for optional chains#792

Merged
graphite-app[bot] merged 1 commit intomainfrom
c/03-15-fix_no-unnecessary-condition_use_resolved_call_types_for_optional_chains
Mar 15, 2026
Merged

fix(no-unnecessary-condition): use resolved call types for optional chains#792
graphite-app[bot] merged 1 commit intomainfrom
c/03-15-fix_no-unnecessary-condition_use_resolved_call_types_for_optional_chains

Conversation

@camc314
Copy link
Copy Markdown
Contributor

@camc314 camc314 commented Mar 15, 2026

fixes #790

@camc314 camc314 marked this pull request as ready for review March 15, 2026 14:36
Copilot AI review requested due to automatic review settings March 15, 2026 14:36
Copy link
Copy Markdown
Contributor Author

camc314 commented Mar 15, 2026


How to use the Graphite Merge Queue

Add the label 0-merge to this PR to add it to the merge queue.

You must have a Graphite account in order to use the merge queue. Sign up using this link.

An organization admin has enabled the Graphite Merge Queue in this repository.

Please do not merge from GitHub as this will restart CI on PRs being processed by the merge queue.

This stack of pull requests is managed by Graphite. Learn more about stacking.

Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Fixes false positives in no-unnecessary-condition for overloaded APIs (e.g., react-hook-form’s getValues(path)) by improving how the rule determines call expression types when validating optional chaining.

Changes:

  • Update call-type extraction to prefer the resolved call-site type for non-optional-chained calls (better overload selection).
  • Add a regression test covering an overloaded getValues(\data.${number}`)` call followed by optional chaining.

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 1 comment.

File Description
internal/rules/no_unnecessary_condition/no_unnecessary_condition.go Adjusts call return type inference to use resolved call-site types when appropriate.
internal/rules/no_unnecessary_condition/no_unnecessary_condition_test.go Adds a regression test for overloaded path-based access followed by ?..
Comments suppressed due to low confidence (1)

internal/rules/no_unnecessary_condition/no_unnecessary_condition.go:756

  • In the optional-chain path (when call.QuestionDotToken != nil or hasOptionalChain(call.Expression) is true), this still derives the return type from the callee type and then picks signatures[0]. For overloaded APIs this can be incorrect because it ignores the resolved overload at the call site (e.g., form?.getValues(data.0) will likely select the wrong overload and can reintroduce the false positive). Consider using ctx.TypeChecker.GetResolvedSignature(callExpr) and GetReturnTypeOfSignature when available, falling back to the current union-of-functions handling only when there is no resolved signature.
			call := callExpr.AsCallExpression()
			if call.QuestionDotToken == nil && !hasOptionalChain(call.Expression) {
				if callType := getResolvedType(callExpr); callType != nil {
					return callType
				}
			}

			funcType := getResolvedType(call.Expression)
			if funcType == nil {
				return nil
			}

			nonNullishFunc := removeNullishFromType(funcType)
			if nonNullishFunc == nil {
				return nil
			}

			// If it's a union type of functions, check each part's return type
			// e.g., (() => undefined) | (() => number) should check if any returns nullish
			if utils.IsUnionType(nonNullishFunc) {
				// For union of functions, check if any function returns a nullish type
				// If so, the optional chaining result can be nullish
				parts := nonNullishFunc.Types()
				for _, part := range parts {
					sigs := ctx.TypeChecker.GetCallSignatures(part)
					if len(sigs) > 0 {
						retType := ctx.TypeChecker.GetReturnTypeOfSignature(sigs[0])
						if retType != nil && isNullishType(retType) {
							// At least one function returns nullish, so use full expression type
							// which includes all possible return types
							return ctx.TypeChecker.GetTypeAtLocation(callExpr)
						}
					}
				}
				// No function returns nullish, get first signature's return type
				signatures := ctx.TypeChecker.GetCallSignatures(nonNullishFunc)
				if len(signatures) > 0 {
					return ctx.TypeChecker.GetReturnTypeOfSignature(signatures[0])
				}
				return nil
			}

			signatures := ctx.TypeChecker.GetCallSignatures(nonNullishFunc)
			if len(signatures) == 0 {
				return nil
			}

			return ctx.TypeChecker.GetReturnTypeOfSignature(signatures[0])

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

You can also share your feedback on Copilot code review. Take the survey.

@graphite-app
Copy link
Copy Markdown

graphite-app Bot commented Mar 15, 2026

Merge activity

graphite-app Bot pushed a commit that referenced this pull request Mar 15, 2026
@graphite-app graphite-app Bot force-pushed the c/03-15-fix_no-unnecessary-condition_use_resolved_call_types_for_optional_chains branch from 0a93370 to be8805a Compare March 15, 2026 14:43
@graphite-app graphite-app Bot force-pushed the c/03-15-fix_no-unnecessary-condition_use_resolved_call_types_for_optional_chains branch from d2e80b1 to 3e5b988 Compare March 15, 2026 14:46
@graphite-app
Copy link
Copy Markdown

graphite-app Bot commented Mar 15, 2026

Merge activity

@graphite-app graphite-app Bot merged commit 3e5b988 into main Mar 15, 2026
8 checks passed
@graphite-app graphite-app Bot deleted the c/03-15-fix_no-unnecessary-condition_use_resolved_call_types_for_optional_chains branch March 15, 2026 14:53
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Bug: no-unnecessary-condition: false positives for react-hook-form with zod integration for array path selector access

2 participants