From c6b01d7f9ed182aed05d1f5cd87fcd3af24149dc Mon Sep 17 00:00:00 2001 From: Fushan Wen Date: Sat, 28 May 2022 15:00:53 +0800 Subject: [PATCH] shell: use `std::find_if` in `ShellCorona::setContainmentTypeForScreen` Consider using std::find_if algorithm instead of a raw loop. (CWE-398) in shell/shellcorona.cpp:1614 --- shell/shellcorona.cpp | 16 ++++++---------- 1 file changed, 6 insertions(+), 10 deletions(-) diff --git a/shell/shellcorona.cpp b/shell/shellcorona.cpp index 6d0c58ca8..ba834ece0 100644 --- a/shell/shellcorona.cpp +++ b/shell/shellcorona.cpp @@ -1573,16 +1573,12 @@ Plasma::Containment *ShellCorona::setContainmentTypeForScreen(int screen, const return oldContainment; } - DesktopView *view = nullptr; - for (DesktopView *v : qAsConst(m_desktopViewForScreen)) { - if (v->containment() == oldContainment) { - view = v; - break; - } - } + auto viewIt = std::find_if(m_desktopViewForScreen.cbegin(), m_desktopViewForScreen.cend(), [oldContainment](const DesktopView *v) { + return v->containment() == oldContainment; + }); // no view? give up - if (!view) { + if (viewIt == m_desktopViewForScreen.cend()) { return oldContainment; } @@ -1641,14 +1637,14 @@ Plasma::Containment *ShellCorona::setContainmentTypeForScreen(int screen, const if (removeAction) { removeAction->deleteLater(); } - view->setContainment(newContainment); + (*viewIt)->setContainment(newContainment); newContainment->setActivity(oldContainment->activity()); insertContainment(oldContainment->activity(), screen, newContainment); // removing the focus from the item that is going to be destroyed // fixes a crash // delayout the destruction of the old containment fixes another crash - view->rootObject()->setFocus(true, Qt::MouseFocusReason); + (*viewIt)->rootObject()->setFocus(true, Qt::MouseFocusReason); QTimer::singleShot(2500, oldContainment, &Plasma::Applet::destroy); // Save now as we now have a screen, so lastScreen will not be -1