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

Parser: improve return detection for switch statments #620

Merged
merged 1 commit into from
Feb 9, 2024

Conversation

danielsan901998
Copy link
Contributor

resolve #619
The switch statement was not handled correctly by nodeIsNoreturn, since break and continue returned .yes like return_stmt that caused that it was treated like a return from the function instead of just exiting the switch statement.
Also added handling for the default statement for a simple switch+default, the case statement is not handled for now because it requires more complex reasoning when combined with multiple cases or with an default statement inside a compound statement, so it return the default .no value.
And changed the check of compound_stmt to try to find a statement with a return instead of just looking the last one.
Since switch_stmt no longer return .complex i have remove it for now, since if_then_else_stmt will no longer propagate it and no other statement use it.
Because the nodeIsNoreturn function is also used to detect the cases of unreachable code, changing break and continue cause the analysis to no longer detect this type of case:

int test(int a){
	switch(a){
		break;
		a=1;
	}
	return 1;
}

This is still not a complete solution since loop statements are not handled correctly, generating a "does not return a value" warning:

int test(int a){
	while(1)
		return 1;
}

@Vexu
Copy link
Owner

Vexu commented Feb 7, 2024

I'd prefer a solution that makes nodeIsNoreturn return false negatives instead of false positives. The loop and switch cases could be changed to return .complex since the current code does not cover anything but the simplest cases.

@danielsan901998
Copy link
Contributor Author

Force-pushed and deleted my changes by mistake when trying to pull changes.
So i'm reopening this again with the changes requested.

@Vexu Vexu merged commit e5855df into Vexu:master Feb 9, 2024
6 checks passed
@Vexu
Copy link
Owner

Vexu commented Feb 9, 2024

Thanks!

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.

Incorrect warning for non unreachable code after switch with break
2 participants