sonar-delphi icon indicating copy to clipboard operation
sonar-delphi copied to clipboard

New rule: Imports should be fully qualified

Open fourls opened this issue 2 years ago • 1 comments

Prerequisites

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

Suggested rule title

Imports should be fully qualified

Rule description

This rule would detect unit imports that are a subset of the actual unit name. For example,

uses SysUtils;

would raise an issue, but

uses System.SysUtils;

wouldn't.

Rationale

There are a number of benefits to fully qualifying unit names, including

  • No ambiguity about which unit is being imported
  • Consistency across the codebase
  • Improved compile time due to not having to look up implicit scopes

A project could remove unit scopes from their .dproj, causing the compiler to be unable to resolve unqualified unit names. This is not always feasible, most obviously when the project compiles third-party source code as part of the compilation process - in times like these a Sonar rule would be useful.

fourls avatar Oct 31 '23 00:10 fourls

Good suggestion, and this would be pretty easy to implement:

  • visit UnitImportNode
  • look at the import declaration (UnitImportNode::getImportNameDeclaration)
  • compare the length against the original unit declaration (UnitImportNameDeclaration::getOriginalDeclaration)

cirras avatar Nov 14 '23 03:11 cirras