[KIO] Modernize the code to use range-for in more places
ClosedPublic

Authored by dfaure on Sep 23 2019, 2:04 PM.

Details

Summary

This was done using clang-tidy's modernize-loop-convert, then
clazy (for qAsConst), then manual cleanups (less auto, better variable
names, etc). Took way longer than I was hoping for...

Test Plan

make && ctest

Diff Detail

Repository
R241 KIO
Branch
2019_09_modernize_range_for
Lint
No Linters Available
Unit
No Unit Test Coverage
Build Status
Buildable 17125
Build 17143: arc lint + arc unit
dfaure created this revision.Sep 23 2019, 2:04 PM
Restricted Application added a project: Frameworks. · View Herald TranscriptSep 23 2019, 2:04 PM
Restricted Application added a subscriber: kde-frameworks-devel. · View Herald Transcript
dfaure requested review of this revision.Sep 23 2019, 2:04 PM

Will see to give this some review later this week (if still needed). Would agree the "modern" code is more easy to grasp by humans and thus better to maintain, so +1 for principle.

All override changes please as separate commit though, unrelated to commit message (IMHO can go in directly). Do we already have agreed on using override with destructors in KF code I do prefer them as well).

Not tested the changes myself, only looked at the code here.

Looks good to me in general, modulo the comments I made.
When it comes to it, my code reading expectations are that it is a real iterator, same like i is an integer index. The current patch proposes to also use it as loop argument name in some places, which I am not sure will help future code readers. I gave some proposals for names to use instead.

Thanks for the work you already put into this, but also please go the full mile and move unrelated changes into separate commits :)

src/core/kcoredirlister.cpp
520–521

it->dir

2303–2304

it->filterExpression/filter/expression?

src/core/kmountpoint.cpp
344

Why change away from auto?

429–430

it -> mountPointPtr? Perfect would be renaming the existing mountpoint to mountPointPath or something, so mountPoint` could be used as loop var name. But I also think this would be too invasive WRT commit history and commit purpose.
Or mp, as used in some other code also looping over the same structure, see below in src/widgets/kpropertiesdialog.cpp

447–450

it->mountPoint

src/core/krecentdocument.cpp
170–171

it->desktopFilePath (hml, that recentDocuments() is semantically quite misleading without reading the docs).

src/filewidgets/kdirsortfilterproxymodel.cpp
119–131

Curious: while touching this, would a static const or constexpr not be the even better option?
Any rule of thumb known when telling the compiler which way to treat this?
Right now unsure what the compiler even does with a plain const array in a method without any optimization. It copies the array data onto the stack on the method invocation, or?

src/ioslaves/help/kio_help.cpp
69–70

missing space after for

src/ioslaves/trash/kio_trash.cpp
503–504

it->info

src/ioslaves/trash/trashimpl.cpp
1093–1094

it -> device

src/urifilters/ikws/kuriikwsfiltereng.cpp
304 ↗(On Diff #66665)

unrelated change, please separate commit

311 ↗(On Diff #66665)

Unrelated change/fix, please separate commit. Though, is this even correct? There is no QString::operator>=(QChar) & similar, is there?

315 ↗(On Diff #66665)

Unrelated, please separate commit. Was the code checked that isNull() is not relevant?

src/widgets/kshellcompletion.cpp
145

While touching all the lines, would improve the code to cache match.value() into some
QString& matchString = match.value(); IMHO.

src/widgets/kurifilter.cpp
681 ↗(On Diff #66665)

I would make this a separate commit, as this does not exactly match the commit message and might confuse future history readers.

src/widgets/previewjob.cpp
760

while touching all the lines & vars, desktopEntryName() could be cached.

774

I would make this a separate commit, as this does not exactly match the commit message and might confuse future history readers.

tests/getalltest.cpp
17 ↗(On Diff #66665)

I would make this a separate commit, as this does not exactly match the commit message and might confuse future history readers.

33 ↗(On Diff #66665)

I would make this a separate commit, as this does not exactly match the commit message and might confuse future history readers.

tests/previewtest.cpp
54 ↗(On Diff #66665)

I would make this a separate commit, as this does not exactly match the commit message and might confuse future history readers.

dfaure marked 18 inline comments as done.Sep 28 2019, 1:03 PM
dfaure added inline comments.
src/core/kmountpoint.cpp
344

Consistency with all other range for loops in kio, and https://wiki.qt.io/Coding_Conventions#auto_Keyword

429–430

picked mp

src/filewidgets/kdirsortfilterproxymodel.cpp
119–131

Good question.

Without optimizations, I assume static means generating atomics for thread-safety, while on stack generates a memcpy indeed.
I checked the assembly on godbolt.org, for that last part. Couldn't see atomics with static because the testcase is all in main(), so no threads involved, but in a library method this is a requirement.

With -O2, the generated code is exactly the same.
https://godbolt.org/z/Ez8fba

So I guess it doesn't matter?

src/urifilters/ikws/kuriikwsfiltereng.cpp
304 ↗(On Diff #66665)

Indeed, this whole subdir was in need of code reformatting.
Done, https://commits.kde.org/kio/c8c2eff2a2362278acddcc995221176a7788e24e

311 ↗(On Diff #66665)

Oh indeed, I thought c was a QChar, given its name...

I guess this creates a QString from the QChar but yeah I'll revert.

324 ↗(On Diff #66665)

the code here uses clear() already for ql elements.

346 ↗(On Diff #66665)

and this is the final use of ql, null vs empty makes no difference =>
https://commits.kde.org/kio/ea035cd204dea8cd563155c4256400fe67e8b69c

src/widgets/dropjob.cpp
67 ↗(On Diff #66665)
src/widgets/kurifilter.cpp
681 ↗(On Diff #66665)
dfaure updated this revision to Diff 66991.Sep 28 2019, 1:06 PM
dfaure marked 4 inline comments as done.

Make changes suggested by Friedrich's code review (thanks!)

kossebau accepted this revision.Sep 28 2019, 10:40 PM

Code changes look all fine to me. Only looked at code here with eyes (tired and fermentation products influenced ;) ), not built, run or tested though.

This revision is now accepted and ready to land.Sep 28 2019, 10:40 PM
dfaure closed this revision.Oct 3 2019, 8:20 AM