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

Rust: Unreachable code query #17525

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open

Rust: Unreachable code query #17525

wants to merge 3 commits into from

Conversation

geoffw0
Copy link
Contributor

@geoffw0 geoffw0 commented Sep 19, 2024

Unreachable code query for Rust.

@paldepind please would you review, particularly whether the logic in firstUnreachable and skipNode is reasonable and written as cleanly as possible right now; and whether there are obvious explanations for the false positive results in the test?

@geoffw0 geoffw0 added no-change-note-required This PR does not need a change note Rust Pull requests that update Rust code labels Sep 19, 2024
Copy link
Contributor

QHelp previews:

rust/ql/src/queries/unusedentities/UnreachableCode.qhelp

Unreachable code

This rule finds code that is never reached. Unused code should be removed to increase readability and avoid confusion.

Recommendation

Remove any unreachable code.

Example

In the following example, the final return statement can never be reached:

fn fib(input: u32) -> u32 {
	if (input == 0) {
		return 0;
	} else if (input == 1) {
		return 1;
	} else {
		return fib(input - 1) + fib(input - 2);
	}

	return input; // BAD: this code is never reached
}

The problem can be fixed simply by removing the unreachable code:

fn fib(input: u32) -> u32 {
	if (input == 0) {
		return 0;
	} else if (input == 1) {
		return 1;
	} else {
		return fib(input - 1) + fib(input - 2);
	}
}

References

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
documentation no-change-note-required This PR does not need a change note Rust Pull requests that update Rust code
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant