How to make ReSharper stop telling to remove if(myString == null) - resharper

I have seen some wired behaviour from the ReSharper and its suggestions. One that particularly made me buff was the one below. Now leave alone that a string can be null, and the suggestions is simply wrong. Can I change some settings to make it stop telling me to remove if-statements that are null checks?

Related

Don't know how to solve an "unresolved reference."

I am 2 hours into my first Kotlin, and any coding, lesson and I am following along with a tutorial.
I have come across an "Unresolved reference" and am not sure how to resolve it so that I can continue following along with my lesson.
There are several options presented, such as: creating local variable, parameter, property and reference.
As stated, I have 2 hours of experience and no idea what these mean yet. Any insight would be appreciated, thanks you.
Screenshot
I'm gonna paste your image in so you can see what I'm talking about - in general, you should always paste code, not screenshots (adding a screenshot as well is fine), but in this case the screenshot is helpful:
See how the else is underlined in red? That's indicating some kind of error, and for a basic language keyword like else, that's a sign you've gone wrong with the syntax - what you've written doesn't make sense, for some reason. Hovering over the red squiggle should give you some info about what the problem could be (it can't always tell for sure, if the code doesn't make sense)
Also, see how everything after the else is greyed out? It's not showing up as code in the normal colours - so it's not that there's code that doesn't make sense, it's not even being recognised as code at all. Again, when a whole section "goes wrong" like that, it's a sign that something's messed up with the syntax, usually something earlier that's causing a problem. And since the else before it is highlighted as a problem, that's a good place to look!
That's just general advice - your issue is this:
if (enteredName == "") { // opening brace for the if block
// some stuff
} // closing brace for the if block
// we just closed the if block - so what's this closing?
} else {
You'll see braces a lot in Kotlin - in simple terms, they wrap a bunch of code into a single block, so when that block is run, it can do several steps. So your if condition, if it evaluates to true, will run everything in that block that follows it. If it evaluates to false, it won't. And in that case, if there's an else statement following it, it'll run whatever follows that instead.
All your if/else stuff is happening inside that click listener you're defining:
submitButton.setOnClickListener {
// some code to run when the button is clicked
}
So those braces hold the code that runs when the listener fires. But what you've written is this:
submitButton.setOnClickListener {
if (something) { // open if
// stuff
} // close if
} // close click listener
// this is now outside the click listener block
else {
That else has to follow an if - but it doesn't, it follows that setOnClickListener line, because that brace closed the lambda (the code block) and ended that line. Now the next line is else { which makes no sense to Kotlin, and that's why it's tripping up, you're getting a red error mark, and the code following it is greyed out because Kotlin doesn't know what to do with it. It's extremely confused.
So you need to make sure your braces are paired up, and each closing brace is in the correct place to close its counterpart. They close in reverse order, right? Basically backing out of each block. Making sure each block is indented an extra level helps, because then you can visually see where each block starts and ends (and it draws a vertical line from the start to the end of each one). If you just remove that extra closing brace, you'll be missing one, because it's just in the wrong place. So you'll have to work out where to put it! Where the listener block ends.
That's uh a really long post to say "one of your braces is in the wrong place" but I hope it helps you understand what's going on and how to spot this kind of thing in the future. You'll have to handle these blocks a lot, so it's good to get used to wrangling them. It's pretty simple once you get it!

Simple debug breakpoint conditions gives me "Unused equals expression"

I don't know what is wrong here. It happened before to me also when I tried conditional breakpoints some weeks before. Do I do something wrong or it's Android studio bug ?
Also when I leave this there another error pop up:
Ok looks like android bug to me or multiple... Conditional breakpoint on field don't work and it works when I do the same thing in function. That warning Unused equals expression persist thou.
So what I did to actually make it work was to create temporary init function, placed call to log something and placed breakpoint with the same condition there.
Unused equals expression warning is quite confusing to me as well as that conditions don't work on fields at last for me, maybe its related that I am running arctic fox preview on M1 mac ? Don't know maybe its just well known bug..
Also that null pointer can be logical because when compiler first gets there commands is actually still null so maybe I could just write condition as notes.createPlayCommands().size == 0 and only real Android Studio bug is that warning of unused equals expression un debug breakpoint view.

prevent resharper from automatically fixing issues

I noticed that Resharper automatically applies fixes (e.g. removing unnecessary namespace prefixes) in methods that I am not working on within the file I'm working on. How do I turn this off? (I only want to fix things if I take specific action).
After reading your question, I'm not sure whether it is about disabling automatic fixes, or just make them more local.
What you're talking about, is called Code Cleanup. You can disable parts of that in the ReSharper options, including the "Optimize Using directives", which you were talking about.
If you just want to cleanup/format a part of a file, you have the option to first select the method/code part, and then hit the shortcut to cleanup. Then, all modifications will just apply to the selected area, not the file.

How to Prevent ReSharper from Wrapping One-Line Statements?

What is the best way to prevent ReSharper from wrapping certain lines of code. For example, an if statement with one condition and a simple body should not be wrapped. ReSharper currently forces this line...
if (item == null) { return null; }
... to break like this:
if (item == null)
{
return null;
}
I don't want to blindly disable this feature necessarily... just make it a bit more intelligent.
Update for newer versions of ReSharper
(my version is 2021.1)
Since a picture is worth than a thousand words, you probably want to configure it like so. In this way it takes care of constructs such as using too:
Check the ones that start with Keep existing arrangement [...] if you want to instruct ReSharper to leave, say, single-line ifs alone if they are already written that way.
Took me longer to figure out than I wanted it to, hopefully it will help someone else.
You can change this in the R# Settings:
Open the menu Resharper -> Options
Go to Code Editing -> C# -> Formatting Style -> Line Breaks and Wrapping
Change to option Break line in single embedded statement to Put on a single line or Do not change
Edit:
As mentioned by #Alexander it would be more suitable to use the option Break Line in a block with a single statement which I think is available since R# 8
In latest Resharper it is under General >> "Wrap log lines"

IDE won't let me type what I want

A couple weeks ago my IDE started getting very forceful... Since it's hard to explain what I mean, here is a video:
http://www.youtube.com/watch?v=tNGMlWwmzpg
It lets me do what I want outside of the parenthesis, so maybe it thinks it's bad syntax two put to evaluations inside, but even if that's true, I don't want my IDE restricting what I can type, it should just use intellisense and put the squigglies.
Here are the extensions I have installed (I disabled the one I installed most recently but it didn't help). I've used almost all of these forever without problem...
update: Offered a bounty because this is making me rage. I have disabled ALL extensions and right now I have the following line:
output += "<div title='" + dr("ServiceGroupType_Desc") + "'>" + dr("ServiceGroupType_Desc") + "</div>"""
I cannot delete the two extra quotes at the end. It just won't let me.
I had the same issue as you and had to uninstall the Codealike plugin to get it working again.
Often when things start misbehaving like this, I find one of three things has happened:
1) Somehow a modifier key gets stuck on. Either my keyboard gets weird, or some software glitch happens. Make sure none of your keys are stuck on unexpectedly.
2) Make sure accessibility services are off (or if you use some of them, make sure only the ones you use are on). Some of these can really mess with you if you are not aware they are on.
3) Make sure your keyboard is in the expected region with the expected layout. Sometimes this switches unexpectedly, and starts messing things up, and I won't realize for a long time.
Sorry if that isn't much help, but it is the only things I can think of.
As you've mentioned, "I have disabled ALL extensions", so it should have nothing to do with any plugin.
Start by turn off IntelliSense
Option->Editor->C/C++->turn off IntelliSense with IntelliSense=FALSE
see if the whole auto complete feature is gone.
"I disabled the one I installed most recently but it didn't help"
You have to make sure the extension is not running in the background. If any, kill them as long as it doesn't affect the VS.

Resources