Conversation
|
Thanks for your pull request and interest in making D better, @gorsing! We are looking forward to reviewing it, and you should be hearing from a maintainer soon.
Please see CONTRIBUTING.md for more information. If you have addressed all reviews or aren't sure how to proceed, don't hesitate to ping us with a simple comment. Bugzilla referencesYour PR doesn't reference any Bugzilla issue. If your PR contains non-trivial changes, please reference a Bugzilla issue or create a manual changelog. Testing this PR locallyIf you don't have a local development environment setup, you can use Digger to test this PR: dub run digger -- build "master + dmd#22840" |
|
So the design for the pragma with parameters was for a struct: module core.lint;
struct LintParams {
bool enabled = true;
bool unusedParams = true;
static LintParams off() => LintParams(enabled: false);
}module mything;
import core.lint;
pragma(lint, LintParams(unusedParams: false));Linters should support other types than just booleans for configuration, and this gives us the ability to change it, and quite importantly, store the struct configured to a project's desires in a single place. module defs;
import core.lint;
enum MyLintParams = LintParams(unusedParams: false);moduler lib;
import defs;
pragma(lint, MyLintParams); |
|
I don't think it's wise to bake linter configuration into the source code, it should be defined at build time via compiler flag IMO.
Compiler flag let your build system apply appropriate strictness per stage: strict in production builds, relaxed in development, skipped entirely for legacy compatibility branches etc. Last thing we want is Other than that, I think it's a great feature. |
@rikkimax Thanks for feedback. I convert this example to tests for coverage. |
Motivation
Currently, developers rely on external tools like D-Scanner to catch semantic anti-patterns and style issues. However, external AST-based linters lack access to the compiler's symbol table and type resolution, which leads to false positives/negatives for complex semantic rules.
At the same time, introducing new semantic checks as compiler warnings (
-w) has historically been rejected because it breaks existing codebases and CI pipelines.Solution
This PR introduces a dedicated, strictly opt-in linter infrastructure integrated directly into the frontend via
pragma(lint).ErrorKind.lintdiagnostic that strictly does not incrementglobal.errorsorglobal.warnings. Compiling with-wor-wewill never fail if a lint rule is triggered.pragma(lint, ruleName)integrates tightly withScopeand applies lexically. It can be toggled on/off (none,all) per-module, per-aggregate, or per-function, respecting D's philosophy of explicit control.SARIFoutput, making it ready for modern IDEs and CI/CD environments.Proof of Concept Rule (
constSpecial)To demonstrate the infrastructure, this PR includes one zero-false-positive rule:
constSpecial. It emits a lint message when special struct methods (opEquals,toHash,opCmp,toString) are declared withoutconst. Since this check sits insemantic3.d(visit(FuncDeclaration)), it leverages the compiler's perfect knowledge of types and correctly ignores compiler-generated thunks (!funcdecl.isGenerated()).Example usage:
Proof of Concept Rule (unusedParams)
Example usage:
Future Impact
If accepted, this lays the groundwork for migrating highly requested, semantically heavy checks (e.g., unused parameters, redundant expressions, catching base
Exceptionor UnusedParams) directly into the compiler, without disrupting the ecosystem or forcing warnings on users who don't want them.