Skip to content
This repository was archived by the owner on Jan 5, 2023. It is now read-only.

UnreachableStatement: tolerate more harmless unreachable return statements#246

Merged
max-schaefer merged 1 commit intogithub:masterfrom
smowton:smowton/feature/nuisance-dead-code-warnings
Jul 7, 2020
Merged

UnreachableStatement: tolerate more harmless unreachable return statements#246
max-schaefer merged 1 commit intogithub:masterfrom
smowton:smowton/feature/nuisance-dead-code-warnings

Conversation

@smowton
Copy link
Copy Markdown
Contributor

@smowton smowton commented Jul 6, 2020

The Golang compiler isn't particularly good at spotting paths that don't need a return statement due to a dominating noreturn statement (e.g. os.Exit(1)), so dead return statements are common. We already tried to tolerate some instances of this pattern; this additionally allows 'true' and 'false' literals, and anything of type 'error'.

The carte-blanche for error values aims to accommodate the pattern "abort(); return whateverErrorWouldOtherwiseBeAppropriate();", which is probably preferable to "return nil", a misleading no-error indication.

Intentionally excluded from this PR: unreachable 'break' and 'continue' statements, which misleadingly indicate control-flow will go somewhere it doesn't.

@smowton smowton requested a review from a team July 6, 2020 14:26
Copy link
Copy Markdown
Contributor

@max-schaefer max-schaefer left a comment

Choose a reason for hiding this comment

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

LGTM, modulo one minor nit. Could you also add a change note? See https://github.com/github/codeql-go/blob/master/change-notes/2020-05-01-bad-redirect-check.md for an example.

or
retval instanceof BasicLit
or
isAllowedReturnValue(retval.(UnaryExpr).getOperand())
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Could you add a test for this? Presumably it's for handling return &SomeStruct{}?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

I don't know what this intends to catch, it was pre-existing

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

What an excellent point. Can you add a test for return &SomeStruct{} anyway?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

It actually wouldn't have caught that (and I'd argue, should not -- they should surely always return nil for a type that supports it), as it previously specified unary-op-of-basic-literal. Troublingly, it looks like -1 is being parsed as unary-minus-of-literal-one rather than literal-negative-one. I'll tighten it up to catch only that case.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Troublingly, it looks like -1 is being parsed as unary-minus-of-literal-one

Ah, right. That is beyond our control (being a decision made by the Go frontend), but definitely worth tightening the predicate.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Updated

…ments

The Golang compiler isn't particularly good at spotting paths that don't need a return statement due to a dominating noreturn statement (e.g. os.Exit(1)), so dead return statements are common. We already tried to tolerate some instances of this pattern; this additionally allows 'true' and 'false' literals, and anything of type 'error'.

The carte-blanche for error values aims to accommodate the pattern "abort(); return whateverErrorWouldOtherwiseBeAppropriate();", which is probably preferable to "return nil", a misleading no-error indication.
@smowton smowton force-pushed the smowton/feature/nuisance-dead-code-warnings branch from a5d0d26 to 5b34c05 Compare July 6, 2020 16:02
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants