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

New rule: Unreachable code should be removed #177

Open
3 tasks done
fourls opened this issue Feb 1, 2024 · 2 comments
Open
3 tasks done

New rule: Unreachable code should be removed #177

fourls opened this issue Feb 1, 2024 · 2 comments
Labels
feature New feature or request rule Improvements or additions to rules

Comments

@fourls
Copy link
Collaborator

fourls commented Feb 1, 2024

Prerequisites

  • This rule has not already been suggested.
  • This should be a new rule, not an improvement to an existing rule.
  • This rule would be generally useful, not specific to my code or setup.

Suggested rule title

Unreachable code should be removed

Rule description

This rule would pick up simple statically verifiable cases of unreachable code, by raising an issue when encountering a statement immediately after the following (i.e. in the same block):

  • Exit
  • Break
  • Continue
  • raise
  • goto

As a gotcha, the rule must not raise an issue if the "dead" statement is a goto label, or contains a goto label nested inside it.

Rationale

Accidental unreachable code is a common problem when writing or refactoring code, and is at best messy and at worst an avenue for bugs and unexpected behaviour. Delphi also has no built in functionality to detect unreachable code, so this would fill a useful niche.

@fourls fourls added triage This needs to be triaged by a maintainer rule Improvements or additions to rules feature New feature or request labels Feb 1, 2024
@zaneduffield
Copy link
Collaborator

Continuing on the label gotcha:

procedure foo;
var
  X: Integer;
label
  Cursed;
begin
  X := 0;
  goto Cursed;
  for X := 0 to 10 do begin
    Cursed:
      WriteLn;
  end;
end;

in this (cursed) code, nothing is actually dead

@fourls
Copy link
Collaborator Author

fourls commented Feb 1, 2024

The cursed code makes a good point 😢
In the PR description,

As a gotcha, the rule must not raise an issue if the "dead" statement is a goto label.

should actually read

As a gotcha, the rule must not raise an issue if the "dead" statement is a goto label, or contains a goto label nested inside it.

I've updated it now.

@Cirras Cirras removed the triage This needs to be triaged by a maintainer label Feb 1, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature New feature or request rule Improvements or additions to rules
Projects
None yet
Development

No branches or pull requests

3 participants