Prevent null forgiving operator usage - c#

As part of the work we do maintaining a project we typically end up discussing the null forgiving operator ! in PR reviews and have been trying to explore ways to prevent it's use altogether.
I am ideally trying to find a way to prevent it's usage through .editorconfig and forcing it to be an error however I have struggled to find any clear options. Is this possible?
If it isn't are there any known other ways of enforcing this desired behaviour?

I had started to write my own Roslyn Analyzer as suggested in comments until a friend pointed me at this repository which has already provided it:
https://github.com/tom-englert/Nullable.Extended
There is an extension or a NuGet Package option which looks exactly like we need.

Related

What is the difference between a Roslyn CodeFix and Refactoring?

I have been scouring all possible documentation I could find about the Roslyn APIs but I could not find an answer to this simple question.
I know a CodeFix inherits from CodeFixProvider and provides a Code Fix.
I also know that a Refactoring inherits from CodeRefactoringProvider and provides a Refactoring.
One difference I found is that for some reason you cannot redistribute Refactorings using Nuget, only through a VSIX, while you can redistribute a CodeFix using both.
But what exactly is a Code Fix and what is a Refactoring? What can one do that the other cannot?
A Code Fix is for where you've identified an error or mistake in the code and can deduce how to correct the code.
A Refactoring is a change to the code that usually will make it neither more nor less correct. It's not unusual to offer multiple refactorings that will transform the code between various forms, including, often, back to the form it was in before someone accepted any refactoring. In contrast, it would be rare in the extreme to find another Code Fix (in the same package) that transformed code into a form that another Code Fix could apply to.
In addition to the previous answer there is another functional difference between code fix and code refactoring. You can easily add support to solution wide code fix by overriding GetFixAllProvider:
public override FixAllProvider GetFixAllProvider() => WellKnownFixAllProviders.BatchFixer;
But as far as I know there isn't such an easy way to provide mass refactoring
The conceptual difference in the accepted answer is also in a way the practical difference.
Both CodeFixProviders and CodeRefactoringProviders generate CodeActions that users can apply to their code. The key difference is that each CodeAction offered by a CodeFixProvider has to be associated with (and is presumed to address) one or more Diagnostics produced by a Roslyn Analyzer, whereas a CodeAction offered by a CodeRefactoringProvider is not associated with any Diagnostic.

How to rename refactor all private member variable variables in a C# VS 2013 project?

I am using camelCase convention for private member variables in my C# projects. Because of changed code quality requirements now I "have to"/"would like to" rename/refactor all these members to _camelCase.
For example logger will be _logger.
Additional info:
1) I definitely know that a particular rename/refactor could cause error, and an interactive refactor tool will warm me in that case. Still I want to automate mass rename, I'll take the risk ending with not compilable source what needs manual corrections.
2) I use latest ReSharper if this helps, but still I can not figure solution for my task. (To be clear I do not suggest ReSharper should be the solution, just a possibility.
Thanks in advance
There is a guy called Steve Cadwallader who has made a visual studio extension called "CodeMaid" (http://www.codemaid.net/). It has several things you can use for cleaning up code etc and the source code is free i think. If you have strict code quality requirements like this maybe its worth it to download his extension and do a slight modification to it. I know it already do stuff with private member variables so it shouldnt be too hard to modify.
This extension can do stuff on a file or project basis which makes it easy for you if you have a large number of files to modify.
One easy way of doing this is to just you Ctrl + F & Find and replace. This however doesn't give you the security that you have only updated the relevant variables and could end up changing other thing. But you can always undo the changes easily if they cause problems.
UPDATE
Having read other answers I have tried Ctrl + R + R and this is now the way I will be doing things. I would up vote that answer but can't as I don't have enough reputation yet!
The only way I found is via the Resharper's "Show Potential Fixes" Menu option as outlined by Jowen here.
How to reformat naming styles with ReSharper?

Can Resharper's code cleanup convert the conditional operator to if/then/else?

In the Cleanup code function, can ReSharper convert all conditional operator statements to if/then/else?
ReSharper 7.1
Update
Why? I don't like them. It is easier to read if/then/else statements.
No research? I did an internet search and looked through all the options, didn't find anything. Cédric Bignon's comment seems to confirm my lack of finding anything. So I thought I'd asked the experts here as I have been using Resharper for 2 days and thought maybe I missed something.
No, there is no such functionality as of 7.1. If you'd like it implemented, please log your request at http://youtrack.jetbrains.com

Finding usages of string == operator in a large codebase

I've had a request to look into the feasibility of replacing all of the string == operator usages in a reasonably large C# codebase with String.Equals() method calls that explicitly specify case-sensitivity.
Haven't had much luck figuring out a way to identify all the occurrences in the codebase, though.
Searching for "==" obviously finds countless instances of types other than strings being compared.
There doesn't seem to be a StyleCop rule to find this.
Nor a ReSharper rule.
As a last resort I tried loading the assemblies into JustDecompile and finding all usages of System.String.op_Equality but that doesn't seem to pick up usages inside of LINQ expressions such as .Where(x => x.StringField == stringField)
So I'm a little stumped and wondered if anyone had any ideas on how to search these pesky comparisons out?
You can use Resharper to find all the usages. Here's what works for me:
Right click on the string type anywhere in your code. Click Go to Declaration.
Resharper will open string.cs from the .NET framework
Scroll down to operator == and right click, select Find Usages
It takes a bit of time but you'll get a nice list of usages, ordered in a tree view.
I tried this with Resharper 6.1 in VS2010.
UPDATE
There is a simpler way to do this:
Select == in a string comparison
Right click on the selection and choose Find Usages Advanced
In the dialog under find check only 'Usages' and set scope to 'Solution' to filter out any references in other libs.
My advise would be to write a very basic and specific code parser which transverses through each scope in your system, recording all string/String variable declarations and detecting any == comparisons using those variables.
Anyone with a deeper knowledge on code parsing is welcome to comment. I'm sure there are some classes/tools out there that one could use.

Comment template in VS2010

I have inherited an API which doesn't have proper comments, and I am working on changing that.
Anyone know if there is some sort of mechanism to add a default XML comments to all the members of a class or an assembly?
(I remember seeing something like that on a webcast and I think he might have used PowerShell script to achieve that.)
This way I can avoid lots of repetitive steps, and have everything in place to go and start writing just the comments.
Anyone has any better suggestions?
GhostDoc is pretty fantastic for XML documentation, although you'll need to purchase a copy to generate automatic documentation for all classes/members. The free version allows you to right click (or use a hotkey) on class or member and it will generate the documentation.
I've found GhostDoc to be pretty good.
Once you've run it over your code you then simply add details where required.
http://submain.com/products/ghostdoc.aspx

Categories

Resources