Skip to content

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

Description

@jorgeluismireles

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Feature/Enhancement RequestThis issue is made to request a feature or an enhancement to an existing one.Unit: CompilerBugs/feature requests, that are related to the V compiler in general.
    No fields configured for Feature.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions