Rule Request: [disable_rationale]
Nobody has claimed this yet.
- Dominant language
- Swift
- Stars
- 19.7k
- Forks
- 2.3k
- Avg merge
- 1d 1h
- Merged PRs (30d)
- 11
Description
New Issue Checklist
- [ *] Updated SwiftLint to the latest version
- [* ] I searched for existing GitHub issues
New rule request
Please describe the rule idea, format
this issue's title as Rule Request: [Rule Name] and describe:
- Why should this rule be added? Share links to existing discussion about what
the community thinks about this.
I would like to have an explanation of why a rule is disabled next to it
- Provide several examples of what would and wouldn't trigger violations.
- would:
// swiftlint:disable missing_docs
- would not:
// This rule is disabled because of blablabla
// swiftlint:disable missing_docs
-
Should the rule be configurable, if so what parameters should be configurable?
No configuration required. -
Should the rule be opt-in or enabled by default? Why?
See README.md for guidelines on when to mark a rule as opt-in.
Opt-in.
Custom rule implementation
At the same time I am proposing this rule, I tried to implement it as a custom rule. I did not managed, and I would appreciate some help. My main idea is:
(1) Find a line containing 'swiftlint:disable' ==> (2) Check that the previous line is a comment
I thought I could do this with a lookbehind regex. The closest I got was the following regex:
custom_rules:
disabled_rules_rationale:
regex: "(?<!\/\/[^\n]*$\n)^[^\n]*swiftlint:disable"
match_kinds:
- comment
message: "All swiftlint disable comments should include an explanation on top"
In https://regexr.com the outcome is the desired one:

But using the same regex in swiftlint does not result the same (note there are no errors):

Some final notes:
- I am using
[^\n]instead of.in the regex because I did not manage to make.work in swiftlint. - The
[^\n]*part of the lookbehind group in the"(?<!\/\/[^\n]*$\n)^[^\n]*swiftlint:disable"regex is the one making things not work: using"(?<!\/\/$\n)^[^\n]*swiftlint:disable"has the following result, which is very close to the intended behaviour:

Thanks!
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start with README.md's opt-in-rules guidance and the custom_rules configuration shown in the issue. Compare the provided violating and non-violating examples, then investigate how SwiftLint processes swiftlint:disable comments and multiline regexes. Done means a rule can require an explanation comment immediately above each disable comment, with no configuration required.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- swift
- Domain
- tooling
- Issue type
- Feature
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 30/100