Is there a way to disable all Resharper warnings for a file or section of code with a single comment? I'm trying to create some coding exercises for interviewing potential candidates, and the Resharper warnings give away the problem I want the candidate to spot :P Suppressing the specific warning still makes it obvious what the problem is.
I still want to have Resharper available during the interview, I just want the candidate to spot this problem without Resharper spoiling the fun.
edit: Sorry I'll try to be more clear about what I'm after. I don't want to permanently disable a particular Resharper warning, I just want it to not show in one particular file, because the point of the exercise is to see if the developer understands the reason for the warning.
To give one example, there is a Resharper warning to use the .Any extension method instead of Count() > 0, which I want the developer to point out themselves. To disable that warning, you have to use a comment of:
// ReSharper disable UseMethodAny.0
around the code. That kind of gives the game away a little.
I was trying to find something like:
// ReSharper disable all
which I could place at the top of the class, so it won't give away what I want the developer to find. But I can't seem to find a way to do that. Using numbers for the Resharper warnings would be fine as well, but I don't think it works that way?
You can press Ctrl + Shift + Alt + 8 to disable analyses and highlightings in the current file.
According to this blog post on the JetBrains blog, in ReSharper 8 there will be a single comment that can disable ReSharper warnings in a file.
It will be
// ReSharper disable All
Note: The "All" is case-sensitive, the rest is not.
The following worked for me.
Add "No Resharper" to Generated Code Regions in R# -> Options -> Code Inspection -> Generated Code
Use the following to suppress the warnings:
#region No Resharper
// All R# warnings are suppressed here
#endregion
You could also use the SuppressMessageAttribute with ReSharper as the category and All as the checkId on the method or the class as shown below. This is more granular than disabling everything in the file if you need the fine grained control.
Tested with Visual Studio 2015 Update 3 and ReSharper Ultimate 10.0.2
[SuppressMessage("ReSharper", "All")]
private void MethodWithMultipleIssues()
{
TestClass instance = null;
// You would get an "Expression is always true" message
if (instance == null)
{
Debug.WriteLine("Handle null");
}
else
{
// You would get an "Code is unreachable" message
Debug.WriteLine("Handle instance");
}
}
you have to configure the ReSharper Inspections
http://www.jetbrains.com/resharper/webhelp/Code_Analysis__Configuring_Warnings.html
You can add the file to the list of "Generated Code" in ReSharpers Options Menu:
Options > CodeInspection > Generated Code
Related
In some unittest I want to refactor some block of code that is duplicate.
So I select a block of code for example the one below:
var timer = Substitute.For<ITimer>();
var alertSender = Substitute.For<IAlertSender>();
var alert = new Alert(NotificationType.Clear, new AlertConfiguration("test", 1, new List<string>()), timer, alertSender);
Unfortunately when resharper does create the method for me it does not detect identical block of code and refactor it for me as well.
Is there any remedy to this issue?
ReSharper doesn't do this, but there's an issue open, please feel free to vote for it: http://youtrack.jetbrains.com/issue/RSRP-274811
ReSharper doesn't do this out of the box, as #citizenmatt mentioned. However, there's a plugin for ReSharper called Agent Ralph that supposedly handles refactoring duplicated code blocks!
You can install it from the ReSharper Gallery if you have ReSharper 8 (it seems the plugin wasn't updated to use v8.2 yet, however).
I have a bigger (c#) WPF application with n-classes and m-methods. I would like to place in every single method a breakpoint, so everytime i press a button in my application or any method gets called, i would like the application in VS2010 to hit that breakpoint. I want to understand the flow/progress of the application.
And since i have many methods i would rather not place manually in every and each of them a breakpoint.
Is there any command or tool to place everywhere in my VS2010 solution a breakpoint?
edit: maybe something like the following addin: http://weblogs.asp.net/uruit/archive/2011/08/04/visual-studio-2010-addin-setting-a-class-breakpoint.aspx
edit2: there are some answers but none of them seems like the straight forward easy solution. Anything else?
EDIT: tested only with C++
I came across this article that shows how to set a breakpoint at the beginning of every method in a class. I've tested it with VS 2010. The basic process (when using Visual C++) is:
Go to Debug > New Breakpoint > Breakpoint at Function (Ctrl + B).
In the Function field, insert MyClass::*
This will show up as a single breakpoint in the Breakpoints window, but as soon as one of MyClass's methods is hit, you'll see a breakpoint at the beginning of every function in MyClass, and all of these will be "children" of the original breakpoint in the Breakpoints window.
I imagine this works with C# as well.
This answer suggests a macro that will do as you ask, but my personal recommendation would be to use a profiler instead - one that lets you pause and resume profiling on the fly (nearly all of the commercial profilers do), and then hit the "Start Profiling" button just before you do your button click. Viewing the call tree in the profiler is often a very convenient way of gaining insight into what an application is doing, much more than stepping through in the debugger.
UPDATE: This feature exists in a Visual Studio extension that I'm working on called OzCode. With OzCode, when you click on the icon next to the class definition, you'll see the QuickAction:
Here's a quick and dirty way to do it using a simple text replace:
Format your C# file so that all of the indentations are lined up. You can do this in Edit > Advanced > Format Document
Open up text replace with Ctrl+H
Set the "Text to Find" field this "^ {".
Set the "Replace" field to this " {System.Diagnostics.Debugger.Break();"
Click the little "Use Regular Expressions" button in the window
Click "Replace All" or hit Alt+A
If your file has any classes with nested enums, classes, or structs, you'll have some compiler errors. Remove the Debug calls from them until your code compiles. If your nested classes have their own methods, you'll have to run this process again with more tabs in the replace strings.
How this works: This uses the Visual Studio document formatter and assumes that all methods in a file start with two tabs and then a "{". So any line that starts with two tabs and a "{" will get replaced with the same two tabs, the same "{", and a call to the Debugger.
If your file has nested enums etc., you'll get compiler errors because the text replace doesn't discriminate between methods and enums. For example, you'll see this:
enum MyColors
{ System.Diagnostics.Debugger.Break(); //error here
Red,
Green,
Blue,
}
If you want the ability to disable these breakpoints, the best way I can think of is a simple bool. Somewhere in your code, insert this:
#if DEBUG
private static bool _ignoreDebug = false;
#endif
(I put the #if DEBUG in there as a flag that this code is only for debugging. It's not necessary) Then in step #4 above, use this replace string instead:
" {if(!_ignoreDebug){System.Diagnostics.Debugger.Break();}"
Then when you hit a breakpoint and don't want to hit any more, in the watch window type this and hit enter _ignoreDebug = true. To turn it back on you'll need to insert a manual breakpoint somewhere that has access to the _ignoreDebug bool.
To remove all of this from your code, either do another text replace, or just edit undo everything.
I think you create an 'aspect' for it using a tool like: postsharp
Aspect oriented programming allows you to add code to the start or end of every method (through a postprocessing step). So it's trivial to add the line:
System.Diagnostics.Debugger.Break()
to every method (without actually editing all your sourcecode).
More typically it is used to add log statements to the beginning of every method like: "Entering method DrawLine(x=30,y=80,z=12)" and at the end of a method: "Leaving method DrawLine(x,y,z)". Which makes following the flow of your program easy
You can use my Runtime Flow extension to see all methods called after press of a button without setting breakpoints.
You can use System.Diagnostics.Debugger.Break() on entry to your method.
Something like this perhaps with a bool that you set at the scope?
#if DEBUG
if (BreakPointEveryMethod)
System.Diagnostics.Debugger.Break();
#endif
There will be a quick way too add this for sure in notepad++ but I am not sure there is a quick and easy way for you to achieve this through a simple command line.
Always, I have got the following message:
name 'byteProivder_LengthChanged' does
not match 'Methods, properities and
events'. Suggested name is
'ByteProviderLengthChanged'
Not even the VS generated method name could get away from this suggestion. For example FormXXX_Load is adviced to change to FormXXXLoad.
So what should I do? Should I follow the name suggestion or just keep the VS style?
If I follow the name suggestion, how to configure the ReSharper and let it change the name automatically?
If I do not follow the ReSharper way, how could I turn this name suggestion option off?
There is a simple way to disable ReSharper's inconsistent naming evaluation check for an entire file/class.
By adding the following comment to the top of the file/class, ReSharper will not evaluate the naming conventions when analyzing the file/class.
// ReSharper disable InconsistentNaming
For a code fragment, you can:
// ReSharper disable InconsistentNaming
private void btnCreate_Click(object sender, RoutedEventArgs e)
// ReSharper restore InconsistentNaming
{
//.....
}
List of suggested comments provided by ReSharper:
// ReSharper disable InconsistentNaming
// ReSharper restore InconsistentNaming
// ReSharper disable CodeCleanup
// ReSharper restore CodeCleanup
ReSharper's advice is (in most cases) useful, but sometimes it misses the target and can even be a bit annoying.
You have three solutions for this;
Edit the ReSharper's definitions to match your liking (this is possible by selecting "edit X rule settings" from the quick fix menu on the left)
Hide the annoying ReSharper message (either locally with a comment or globally by changing the settings for this type of message. Both are possible from the quick fix menu)
Ignore ReSharper's message when it's simply useless.
No matter what you choose, make sure your selection encapsulates your entire work (as well as your team's work if you're a part of one). For instance, in the case of option 3, make a list of situations where ReSharper is ignored.
Keeping your code consistent is vital to any project (be it small or large) and should be your first guideline when thinking about ReSharper.
Personally, I suppress those warnings and then it ignores them.
If you go to ReSharper > then choose Inspection Severity in the Code Inspection menu, you can switch this off.
To turn the suggestion off, you can change ReSharper's Inspection Severity.
ReSharper > Options > Code Inspection > Inspection Severity > Inconsistent Naming
You can also change or create custom Naming Styles for individual languages.
ReSharper > Options > C# > Naming Style > Advanced Settings
More information about creating custom Naming Styles can be found on JetBrains' and on devloq.
I do not follow all r# suggestions. The one I don't like I change, the one that can't be changed I turn it off (we have our own guidelines).
You can suppress a single warning instance by adding the following comment before the line with the warning, for example, for the following warning:
Name 'Counter' does not match rule 'Local Function'. Suggested name is 'counter'.
Disable like this:
// ReSharper disable once InconsistentNaming
function Counter() {
this.pending = 0;
this.succeeded = 0;
this.failed = 0;
}
You can also use StyleCop for ReSharper and define your own rules or modify default behaviour to follow your style.
If you want to turn off some code inspections, i think, the easiest way is to choice "Inspections options for...." from the menu that shows on the right side in editor (for that particular error/warning). Then a popup will show up and you can change the behavior of that type of inspections. Last option:"Do not show" does the trick.
This applies to R# 5.0 and probably 4x as well.
In general, you should use the coding standards that you think will fit your needs.
ReSharper is trying to make you adhere to Microsoft's own naming guidelines which is a good idea, especially if your team is not using any naming conventions already. Using a naming convention gives your code greater readability and a more consistent API.
Here is the screenshot just in case you are having problem looking for ReSharper settings:
Upon clicking Resharper Menu -> Options:
After namespace, class, struct, enum, for, foreach, while, switch, do, using, unchecked and at the beginning of methods, shouldn't Visual Studio automatically include curly brackets? How to activate this feature?
With Resharper curly closing bracket adds by itself (either right away or after pressing enter). I think it's also the case for non-resharped Visual Studio but not 100% sure about that. Only opening bracket you have to write by yourself.
Currently Resharper 5.0 is free (as long as it's in beta/night build status), you just have to reinstall it every 20-30 days (but you would want to do it anyway since they tend to fix/add things). I can't code without Resharper anymore :-) It's so much time saver! Not sure what i will do when it's no longer beta ;/
Edit:
With Resharper you can even get IDE to use braces or not to use them in some cases (where it's possible). Check out this link about code formatting in Resharper.
One option is to use Code Snippets. Visual Studio already comes with snippets for namespace, enum, for, foreach, while, switch, do, using, and unchecked. For instructions on how to use snippets go here.
If you can think of a snippet that makes sense for your class and struct you can create your own snippet.
Devexpress have tools for visual studio
CodeRush
The IDE can't be sure if you don't want to create a one liner.
Resharper does make things easier though. If you type "{" and press enter the "}" will automatically be generated.
If you press CTRL+K, CTRL+S this will give you a snippet of options in which it will automatically create whichever option you select.
It is very easily and you have not to use R#.
Only write "if, else, for, foreach..." and then press TAB. Curly brackets will be automatically created and your cursor jump to round brackets. If in round brackets is more controls (e.g. for(control1; control2; control3)), then you can switch between this controls with press TAB. If you finished with changing controls, then you can press enter and cursor will jump to body of curly brackets.
But if do you want write new "method, class,..." I usually use general shortcut "ctrl+.".
e.g. Write to some method (e.g. MyMethod) call new method (e.g. MyNewMethod) which not be created yet:
private void MyMethod()
{
string[] a = this.MyNewMethod(5, "something");
}
Then set cursor to "MyNewMethod", press "ctrl+." and press enter.
New method will be created automatically on the end of this class:
private void MyMethod()
{
string[] a = this.MyNewMethod(5, "something");
}
private string[] MyNewMethod(int v1, string v2)
{
throw new NotImplementedException();
}
For namespace, class, struct, enum and switch, you're right. The syntax forces you to get an opening brace, but I don't think you can force VS to do this (without using an add-in).
In the other cases, an open brace is optional, and some people want NOT to have the brace if they have only one statement afterwards. So, VS could not add a brace automatically even if it could.
My team is using FxCop to help clean up an existing ASP.NET application.
We have noticed some strange behavior in the way FxCop counts warnings.
It seems that on one pass through the code, FxCop only finds and counts the first warning related to a specific rule in each method.
So, if I have:
public test3(){
int a = 0; //DoNotInitializeUnecessarily
int b = 0; //DoNotInitializeUnecessarily
}
...my FxCop report will only find and count the first warning of type DoNotInitializeUnecessarily in method test3(). Is there any way to make FxCop find and count both instances of this problem in method test3()?
The current method of counting is problematic for us, because FxCop is under reporting the number of warnings. This makes it difficult to estimate how much time will be required to fix existing FxCop warnings, since we don't actually know how many are in the application.
Did you try changing
Tools->Settings->Project Defaults->"Disable rules after [ 1] exceptions"
?