Unused Variable Warning in Swift Suggested Fix Is a Bit Annoying?

If I'm just typing a variable out like:


var someURLs = Array<URL>()


I get a compiler warning, with a suggestion that will change the code to:


_ = Array<URL>()


I just typed it out, I'm about to use the variable in a couple of seconds, so I really don't want to use the suggested fix.


In Objective-C we get a warning, which is useful so you don't forget, but don't get hinted to do something we don't want to do.


The compiler seems pretty aggressive in trying to get you to fix things, making suggestions that make no sense because you are still writing the code?

Replies

I recommend you file a bug about this. It’s a complex UI issue — you want issues to show to show up quickly, but not too quickly — and it’d be good for you to register you opinion on how that balance should be struck.

Please post your bug number, just for the record.

btw This issue has been the subject of an interesting swift-evolution conversation recently.

Share and Enjoy

Quinn “The Eskimo!”
Apple Developer Relations, Developer Technical Support, Core OS/Hardware

let myEmail = "eskimo" + "1" + "@apple.com"

Cool. Thanks for the info. I'll have to file a bug on this later. The issue described on that linked page is pretty much how I was feeling.


There were some suggestions by the compiler, like the unused variable suggestion that I felt for the most part were unnecessary (suggesting to do something that is only "correct" from the compiler's perspective but the compiler doesn't have enough context to make such a decision b/c I'm still typing).


I have a habit of taking the suggested fixes in ObjC because the compiler is usually right. In some cases I think it'd probably be enough to just leave the warning with no suggested fix.

Wouldn't the best time for that warning to show up be when you move the cursor out of the func you are in? That is most likely the first time it would be worth drawing the dev's attention to an unused variable.

Yea...but it isn't the warning that's the annoying part (at least not to me). I want warnings when my code is wrong (even while I'm still typing, I don't care) It's the *suggested fix* that's the annoying part. You click to let Xcode to autocomplete your code only you don't get what you want. Sometimes I believe Xcode will suggest code that may give you an error.


In ObjC, Xcode almost always does the right thing. I'm sure that's because ObjC's been around forever and is more mature at this stage I guess.