Any way to make a change to every line of code with the same compiler warning?

Viewed 179

I recently became in charge of updating a large portion of code. One of the things I was asked to do was to reduce the number of compiler warnings, as since there were so many it was making it difficult to figure out what was important.

I have about 700 warnings that say: "The variable XXXXX is assigned but its value is never used", and sure, I could go and fix every line by hand, but this isn't the only type of warning I have.

What I was wondering is if there was a tool (in Visual Studio or otherwise) I could use to detect a specific warning type, and make an edit to every line of code that has that warning.

I googled around a little, but couldn't find anyone with a similar problem. Maybe someone could point me in the right direction?

2 Answers

Yes, there is a way. You need Visual Studio 2015 or 2017 for that.

First, open a file with a particular warning you're interested in.

Go to the line of code that produces that warning and press Ctrl + ..

You will see a Code Fix window:Code Fix window

Click on the Fix all occurrences in Solution link.

You'll get a window that lets you preview the changes.

Finally, click the Apply button.

#pragma warning can enable or disable certain warnings.

#pragma warning

#pragma warning disable CS0219 // Variable is assigned but its value is never used
            //here goes the code with the variables which are never used....
#pragma warning restore CS0219 // Variable is assigned but its value is never used

With the #pragma warning you can keep you variables for future purpose.

Related