Check identifier spelling against blacklist?

SQ 8.6.0.

There are regex-based rules for checking the orthography of identifiers, e.g., whether a class name uses PascalCase. Some of them use specific strings, such as java:S2047, which looks for ‘is’ or ‘has’.

Is there a way to look for forbidden words rather than required words? In other words, I’d like to check all identifiers (not just one category, like class name, but anything) against a blacklist of forbidden spellings. Mainly we’re looking for common abbreviations we’d rather not use, for consistency. As a made-up example, say we have a project where some people use EOF as an abbreviation for “end of file” and others use EOFile. We’d like everyone to use the former, so we’d like to flag the latter.

Of course, that could be searched with a script, but we’d like the “improper” use to be flagged right away by SonarLint.

Seems this (Black Lives Matter ruleset) would be one application of this more general approach.

1 Like

This would be more flexible if the regex itself could have exceptions, to avoid flagging an OK word that happened to have a forbidden word as a substring (e.g., the crosslink posted above has an example of blocking “master” while permitting “headmaster”).

To add to my OP, in principle one could modify the existing regex to make an existing rule block forbidden words, but in practice this would make the regex horribly inefficient.

It would also be nice to have some kind of hierarchy. For instance, forbid some terms only in a class name, while other words are forbidden in any identifier (class, method, local variable) or even in a comment.

There already is an existing rule, java:S1190, which checks for future keywords in order to future-proof one’s code. Perhaps this rule should be expanded to a parameterized rule with a regex, with the default regex set to the list currently hardcoded in S1190 (which I believe is just underscore and ‘enum’?).

(Related to Custom Rule for avoid keyword