Fix incorrect use of case statements in accountmodel.cpp
AcceptedPublic

Authored by bero on Feb 20 2020, 12:01 AM.

Details

Reviewers
davidedmundson
Group Reviewers
Plasma
Summary

Building user-manager 5.18.1 with clang++ 10.0 resulted in 2 compiler warnings - a quick look at the code shows they're real bugs.
"case Qt::DisplayRole || AccountModel::FriendlyName:" isn't quite the same as "if(role==Qt::DisplayRole || role==AccountModel::FriendlyName)" -- but that's clearly what is meant here.

Test Plan

Check the output of AccountModel::data() with various role types, including Qt::DisplayRole, AccountModel::FriendlyName, Qt::DecorationRole and AccountModel::Face before and after applying the patch.

Diff Detail

Repository
R128 User Manager
Lint
Lint Skipped
Unit
Unit Tests Skipped
bero created this revision.Feb 20 2020, 12:01 AM
Restricted Application added a project: Plasma. · View Herald TranscriptFeb 20 2020, 12:01 AM
Restricted Application added a subscriber: plasma-devel. · View Herald Transcript
bero requested review of this revision.Feb 20 2020, 12:01 AM
bero edited reviewers, added: Plasma; removed: Frameworks.
davidedmundson accepted this revision.Feb 20 2020, 12:04 AM
This revision is now accepted and ready to land.Feb 20 2020, 12:04 AM
bero added a comment.Feb 20 2020, 12:11 AM

Actually the obvious fix doesn't work (thought it was so obvious that I submitted it before testing, sorry) because Qt::DisplayRole and AccountModel::FriendlyName are both 0 (-> duplicate case value) and Qt::DecorationRole and AccountModel::Face are both 1 (-> duplicate case value). Also explains why the bogus variant before "worked" - (Qt::DisplayRole || AccountModel::FriendlyName) = 0 || 0 = 0 = Qt::DisplayRole = AccountModel::FriendlyName and (Qt::DecorationRole || AccountModel::Face) = 1 || 1 = 1 = Qt::DecorationRole = AccountModel::Face

Revising the patch...

bero updated this revision to Diff 76032.Feb 20 2020, 12:19 AM

Probably best to just add a comment explaining what needs to be done if AccountModel::* roles ever diverge from DisplayRole/DecorationRole...

Leaving the code the way it is now will result in a harder to trace error if Qt ever changes the values of DisplayRole or DecorationRole (and may give people looking at the code bad ideas), revising it the way I proposed earlier causes duplicate case values.

bero added a comment.Feb 20 2020, 12:21 AM

Another option would be switching the code to use if instead of switch/case so the || operator would actually do what the code expected (and x || x, unlike duplicate case values, isn't an error)