How to create a modify an existing rule

Hey all. I am on version 6.7.6 (free).

I am following instructions based on documentation: SQ Rules

I am trying to modify an existing rule which does not seem possible. Instead, you should create a rule from an existing one. That sounds fine but I do not see any C# templates. I have selected the Show Template Only filter and all I gain access to is 13 templates none of which are C#. Shouldn’t there be more?

Capture

Is there something I am doing wrong or maybe there are no C# templates? Any help would be appreciated.

Hi @smyrin

I am trying to modify an existing rule which does not seem possible.

What you trying to achieve?
If you don’t like something about some rule you can

  • change rule parameter(s) value if any
  • disable the rule

Note that both actions will require using custom rule profile (not “Sonar way”, as it’s a default profile you can not change).

And indeed there is no template rules available for C#.

Lena

Thanks for replying Elena.

I want to modify the rule: Types should be named in PascalCase to allow for some special conditions such that “class MyClassDTO” is compliant. I am imaging that I can change something in the way the rule invokes (such as a regex expression) that will make it work the way I want. Optionally, I can create a new rule. I don’t understand how to do that.

When I am on the Rules tab, I have the rule selected and my custom quality profile (not the default one). The Change button only allows me to change the severity of the rule, not the rule itself. The Extend Description button allows be to add description (I think). What I don’t see is how to edit the rule or even to see how it works under the covers.

If I follow the doc instructions, it says to select the Show Templates Only filter. This shows 13 templates. I was expecting more but perhaps that was wrong. Why am I only seeing 13 templates? Is that all that is needed to produce all the rules in SQ?

Continuing…I can create a new custom rule from a template - that works but I really don’t know what I am doing just yet. I wanted to see the how the Types should be named in PascalCase rule was created so that I could just tweak it a little. For example, I could create a new rule based on the Track uses of disallowed classes but I need to come up with a regex pattern. Where can I find the regex pattern used by the Types should be named in PascalCase rule so I don’t have to do this from scratch?

Any help would be appreciated.

You can not change rule “Types should be named in PascalCase”, this rule is not configurable.
And again, there is no template rules for C#.

So I see 3 options for you

  • Rename your class to MyClassDto to be compliant with the rule (and IMO lowcasing abbreviation is a good way to go)
  • Deactivate rule (then no naming rules will be checked)
  • Create your own rolsyn analyzer with a rule you want. And then import results of that rule as external. See documentation here https://docs.sonarqube.org/pages/viewpage.action?pageId=11640944

Thanks for the options. Out of curiosity, why are there no C# templates? Is that a paid version exclusive? And if no C# templates, then how are the C# files analyzed? Where are the rules?

Sorry for the delay @smyrin. You can check the rules implementation on our github repo. Our code is open source. Our analyzers are Roslyn analyzers, and you can create a Roslyn analyzer as well (see Microsoft docs)