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

Warn about dead code within match and if/else #23404

Open
2 tasks
jorgeluismireles opened this issue Jan 8, 2025 · 0 comments
Open
2 tasks

Warn about dead code within match and if/else #23404

jorgeluismireles opened this issue Jan 8, 2025 · 0 comments
Labels
Feature/Enhancement Request This issue is made to request a feature or an enhancement to an existing one. Unit: Compiler Bugs/feature requests, that are related to the V compiler in general.

Comments

@jorgeluismireles
Copy link

jorgeluismireles commented Jan 8, 2025

Describe the feature

A programmer mistake can produce dead code if uses within if/else branches comparisons like a == a and within match like match a { a { ... } else { ... } }.

The compiler could look for these types of comparisons and issue warnings about the possibility of dead code.

Use Case

Dead code with if and else:

fn f1(a int) string {
	if a == a {
		return 'a' // always
	} else if a == a {
		return 'a again' // never happens
	} else {
		return 'other' // never happens
	}
}

Dead code within match:

fn f2(a int) string {
	return match a {
		a { 'a' } // always
		1 { '1' } // never
		else { 'else' } // never
	}
}

match is more robust than if/else since already prevents repeated constants and exhaust the search with mandatory else. But with variables or post-calculated constants also can produce dead code. Some examples are in issue #23403

Proposed Solution

No response

Other Information

No response

Acknowledgements

  • I may be able to implement this feature request
  • This feature might incur a breaking change

Version used

V 0.4.9 9fc8352

Environment details (OS name and version, etc.)

Playground

Huly®: V_0.6-21835

Note

You can use the 👍 reaction to increase the issue's priority for developers.

Please note that only the 👍 reaction to the issue itself counts as a vote.
Other reactions and those to comments will not be taken into account.

@felipensp felipensp added Feature/Enhancement Request This issue is made to request a feature or an enhancement to an existing one. Unit: Compiler Bugs/feature requests, that are related to the V compiler in general. labels Jan 9, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Feature/Enhancement Request This issue is made to request a feature or an enhancement to an existing one. Unit: Compiler Bugs/feature requests, that are related to the V compiler in general.
Projects
None yet
Development

No branches or pull requests

2 participants