Skip to content

PoC_add_new_pragma_lint_v2#22840

Draft
gorsing wants to merge 2 commits intodlang:masterfrom
gorsing:poc_v2_pragma_lint
Draft

PoC_add_new_pragma_lint_v2#22840
gorsing wants to merge 2 commits intodlang:masterfrom
gorsing:poc_v2_pragma_lint

Conversation

@gorsing
Copy link
Copy Markdown
Contributor

@gorsing gorsing commented Mar 30, 2026

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).

  • Zero Breakage: Lint messages use a new ErrorKind.lint diagnostic that strictly does not increment global.errors or global.warnings. Compiling with -w or -we will never fail if a lint rule is triggered.
  • Lexical Scoping: pragma(lint, ruleName) integrates tightly with Scope and applies lexically. It can be toggled on/off (none, all) per-module, per-aggregate, or per-function, respecting D's philosophy of explicit control.
  • Tooling Integration: Lint diagnostics are fully compatible with the existing SARIF output, 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 without const. Since this check sits in semantic3.d (visit(FuncDeclaration)), it leverages the compiler's perfect knowledge of types and correctly ignores compiler-generated thunks (!funcdecl.isGenerated()).

Example usage:

pragma(lint, constSpecial):

struct S {
    // Triggers: "Lint: special method `opEquals` should be marked as `const`"
    bool opEquals(ref const S other) { return true; } 
}

pragma(lint, none): // Disable for subsequent declarations

Proof of Concept Rule (unusedParams)

Example usage:

pragma(lint, unusedParams):

class A
{
    void foo(int x)
    {
    }
}

Future Impact

If accepted, this lays the groundwork for migrating highly requested, semantically heavy checks (e.g., unused parameters, redundant expressions, catching base Exception or UnusedParams) directly into the compiler, without disrupting the ecosystem or forcing warnings on users who don't want them.

@gorsing gorsing requested a review from ibuclaw as a code owner March 30, 2026 14:05
@dlang-bot
Copy link
Copy Markdown
Contributor

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 verify that your PR follows this checklist:

  • My PR is fully covered with tests (you can see the coverage diff by visiting the details link of the codecov check)
  • My PR is as minimal as possible (smaller, focused PRs are easier to review than big ones)
  • I have provided a detailed rationale explaining my changes
  • New or modified functions have Ddoc comments (with Params: and Returns:)

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 references

Your 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 locally

If you don't have a local development environment setup, you can use Digger to test this PR:

dub run digger -- build "master + dmd#22840"

@gorsing gorsing marked this pull request as draft March 30, 2026 14:06
@gorsing gorsing changed the title Refactoring add pragma lint v11 (#81) PoC_add_new_pragma_lint_v2 Mar 30, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants