How can I turn a bad rule good? - sonarqube

Our SonarQube project is reporting violations across our codebase, because we have braces at the start of line instead of end of line.
Right or wrong, this is our accepted style. So how can we remove the end of line requirement and add a start of line requirement, and modify it a bit for else statements as
if()
{
} else {
}

Assuming it's a Java Project, you may look for rules related to braces.
To see all java related rules dealing with braces, use:
https://yoursonarhost/coding_rules#languages=java|q=braces
From your example, squid:LeftCurlyBraceStartLineCheck seems the wright rule.
Also possible to search with filter on specific Quality Profile, f.e.:
https://yoursonarhost/coding_rules#qprofile=java-sonar-way-74224|activation=true|q=braces
Then deactivate the rule that doesn't work for you or change it's severity. If no appropriate rule exists you may roll your own, based on those other brace rules, see sources:
Sonarsource Java Analyzer sources

Related

How to trigger a static analyzer warning on a specific code path?

I'm looking for a way to trigger a static analyzer warning on specific code path that would normally not happen or indicate an unhandled error condition.
For example, I would like do write something like this:
int err = some_internal_call(arg1, arg2);
if (err == errInternalError) {
analyzer_assert();
}
Then when I run the analyzer, and it analyze all the different code path, it would warn me that the analyzer_assert() has been called.
Specifically looking into the clang analyzer, but I'm also curious if other analyzer have this feature.
It looks like I could add this feature to clang by write a custom checker, but I'd rather not have to do it myself.
Many analyzers have diagnostics, warning that conditions are always false or true. To find such situations you can use many different tools. But apparently you need another thing. You want to find always true/false conditions because of which a function with a certain name is always/never called, don't you?
If yes, most likely, there is no a ready-made solution. You need to either implement such functional yourself using Clang or you can reach out to someone who can do such a custom service for you. For example, our team implements similar customer-specific rules in a PVS-Studio analyzer. Such diagnostics are included in a separate group and are disabled by default. However, you can always enable and start using such diagnostics.

SONAR COBOL rule: "Procedures should share a naming convention in a PERFORM procedure THRU 'prefix'procedure"

want to code -EXIT suffix rather than END- prefix, but can't delete prefix. any thoughts appreciated. thanks.
This Sonarqube Rule has a default convention of 'END-' as prefix. But as it is documented, it can be changed by setting the parameter suffix as '-EXIT'.
This is a known issue which should be fixed in the next version.
Why not remove the troublesome and error causing exit paragraphs and related perform/thru constructs and just go to a straight perform everywhere? You can get the same effect and remove a source of hard to detect, annoying bugs.

Adding keyword commands and functions to Textmate 2 bundle syntax

I want to add some additional syntax highlighting definitions to an existing bundle, but I need some general advice on how to do this. I'm not building a syntax from scratch, and I think my request is pretty simple, but I think it involves some subtleties for which I find the manual somewhat impenetrable in finding the answer.
Basically, I'm trying to fill out the syntax definitions for the Stata bundle. It's great, but there is no built in support for automatically highlighting the base commands and the installed functions, only a handful of basic control statements. Stata is a language which is primarily used by calling lots of different high level pre-defined command calls, like command foo bar, options(). The convention is that these command calls be highlighted.
There are a ton of these commands, and stubs which are used for convenience. Just the base install has almost 3500. Even optimizing them using the bundle helper, which obviously gets rid of the stub issue, still yields a massive regex list. I can easily cut this down to less than 1000 important ones, but its still a lot. There are also 350 "functions" which I would like to match with the syntax function()
I essentially have 3 questions:
Am I creating a serious problem by including a very comprehensive list of matching definitions?
How do I restrict a command to only highlight when it either begins a line or there is only whitespace between the beginning line and the command
What is the preferred way of restricting the list of functions() to only highlight when they have attached parentheses?

VS2010 / Code Analysis: Turn off a rule for a project without custom ruleset

...any change?
The scenario is this:
For our company we develop a standard how code should look.
This will be the MS full rule set as it looks now.
For some specific projects we may want to turn off specific rules. Simply because for a specific project this is a "known exception". Example? CA1026 - while perfectly ok in most cases, there are 1-2 specific libraries we dont want to change those.
We also want to avoid having a custom rule set. OTOH putting in a suppress attribute on every occurance gets pretty convoluted pretty fast.
Any way to turn off a code analysis warning for a complete assembly without a custom rule set? We rather have that in a specific file (GlobalSuppressions.cs) than in a rule set for maintenance reasons, and to be more explicit ;)
There is no way to create an assembly-level exclusion that will cover all violations of that rule for types and/or members within the assembly.
You could probably still use the CodeAnalysisRules element in your project file, but this is essentially just as much work as a custom ruleset, and more difficult to track given that it's not shown in the project properties UI.
Regardless of the mechanism you would prefer to use, you should also consider whether you want to simply exclude existing violation or whether you want new violations to be introduced. If the former, you should add SuppressMessage attributes for the existing violations. If the latter, you should disable the rule for the assembly.
BTW, in case you weren't aware of this, you can suppress multiple violations at once in the violation list in VStudio.
You'd actually have more flexibility of exclusions with CodeIt.Right for static analysis. And saved all that time :)

how to match function code block with regex

What I like to do is remove all functions which has specific word for example, if the word is 'apple':
void eatapple()
{
// blah
// blah
}
I'd like to delete all code from 'void' to '}'.
What I tried is:
^void.*apple(.|\n)*}
But it took very long time I think something is wrong here.
I'm using Visual Studio. Thank you.
To clarify jeong's eventual solution, which I think is pretty clever: it works, but it depends on the code being formatted in a very particular way. But that's OK, because most IDE's can enforce a particular formatting standard anyway. If I may give a related example - the problem that brought me here - I was looking to find expressions of the form (in Java)
if (DEBUG) {
// possibly arbitrary statements or blocks
}
which, yes, isn't technically regular, but I ran the Eclispe code formatter on the files to make sure they all necessarily looked like this (our company's usual preferred code style):
if (DEBUG) {
statement();
while (whatever) {
blahblahblah(etc);
}
// ...
}
and then looking for this (this is Java regex syntax, of course)
^(\s*)if \(DEBUG.*(?:\n\1 .*)*\n\1\}
did the trick.
Finally did it.
^void.*(a|A)pple\(\)\n\{\n((\t.*\n)|(^$\n))*^\}
Function blocks aren't regular, so using a regular expression in this situation is a bad idea.
If you really have a huge number of functions that you need to delete (more than you can delete by hand (suggesting there's something wrong with your codebase — but I digress)) then you should write a quick brace-counting parser instead of trying to use regular expressions in this situation.
It should be pretty easy, especially if you can assume the braces are already balanced. Read in tokens, find one that matches "apple", then keep going until you reach the brace that matches with the one immediately after the "apple" token. Delete everything between.
In theory, regular language is not able to express a sentence described by context free grammar. If it is a one time job, why don't you just do it manually.
Switch to VI. Then you can select the opening brace and press d% to delete the section.

Resources