Tossing around the idea for this QtCreator "Quick Fix"....
-
I have seen, on occasion, the detach warning when people write:
// assuming that: QList<QSomeType> someList; // or whatever // This... or an iterator loop with similar issues... for (const QSomeType &someVar : someList) someVar.doneQuestionably(); // or somevar-> depending on decoratorsI noticed that 99% of these I stumble upon are easily fixed:
// ..that could be simply solved by: foreach (const QSomeType &somevar, somelist) someVar.doneRight();We all know this, yes of course we do. But it still stands that some do not, which is why there is a nice warning from CLazy about it.
What I'd like to know is, what do you think about this solution being added to quick fixes? I mainly am interested in it because we could probably reduce the total global presence of these kinds of loops for reasons(see list below) and maybe because they simply didn't realize (or wanted to take the time to find out) that they could.
List of reasons (common to least):
- turned off CLazy or does/can not use it
- has become "numb" to this warning and their brain physically sees "nothing serious, code on..."
- feels that since the object is a Qt type (somehow) it can't possibly cause any issues or do anything bad
- wrote their code so fast and never came back that Clazy didn't have a chance to even complain :3
- convinced that they want "real" loops and need range loops (but there ain't no way they're ever going to love them)
- the idea that using foreach is not as clean as using ranged loops because it is not using a native expression or keywords
- some people just don't like macros at all, but are kidding themselves if they actually think can avoid them while using Qt
So what do ya think? Quick fix feasible? My one concern is that not every situation (esp for non-qt stuff) is going to be fixable, but for the most part, if you are using C++ in QtCreator, are a beginner/novice, you are very likely using Qt objects rather than writing plain C++ code.
This only eats at me each day because I've fixed so many of these in foreign code I've lost count. And I'm not talking old, legacy code, I am talking code that was recently written with modern Qt versions just shy of the bleeding edge. It boggles my mind, but then maybe I'm just having a case of POCPD (Programmer's Obsessive-Compulsive Programming Disorder which is another story for another time).
-
You don't have deprecation warnings on, or you would not suggest foreach :P
Don't use it, I'm surprised its still in the library
I would suggest simply writing the c++ way with std::as_const
Or if you insist on "simpler" macros:
#include <utility> // std::as_const #define FOREACH_CONST(var, container) \ for (const auto& var : std::as_const(container))