Skip to content

fix(dde-apps): return empty list when firstNItems count <= 0 - #1715

Draft
MyLeeJiEun wants to merge 2 commits into
linuxdeepin:masterfrom
MyLeeJiEun:fix/dde-144-firstnitems-count-le0
Draft

fix(dde-apps): return empty list when firstNItems count <= 0#1715
MyLeeJiEun wants to merge 2 commits into
linuxdeepin:masterfrom
MyLeeJiEun:fix/dde-144-firstnitems-count-le0

Conversation

@MyLeeJiEun

@MyLeeJiEun MyLeeJiEun commented Aug 26, 2026

Copy link
Copy Markdown
Contributor

问题描述

dde-shellapplets/dde-apps/itemspage.cpp 方法 ItemsPage::firstNItems(int count) 存在语义缺陷count = 0(及任意 count <= 0)时返回首项而非空列表,违反 firstNItems(0) 应返回空的直觉契约。

缺陷根因

循环内result.append(item) 再判 result.count() >= count

  • count = 0 时:第一次迭代 append("a")result.count() 变 1 → 1 >= 0 为 true → 返回 ["a"](首项),而非预期的空列表。
  • 同理 count < 0(如 -1)也返回首项(1 >= -1 为 true)。

修复内容

QStringList result; 之后、循环之前新增早返逻辑,对 count <= 0 直接返回空列表:

QStringList ItemsPage::firstNItems(int count)
{
    QStringList result;
    if (count <= 0)
        return result;

    for (const QStringList & pageItems : std::as_const(m_pages)) {
        for (const QString & item : pageItems) {
            result.append(item);
            if (result.count() >= count) {
                return result;
            }
        }
    }

    return result;
}

不采用「先判后 append」方案——会改变 count 恰好为页边界时的返回项数(少取一项),引入回归。

修复效果

  • firstNItems(0)[] ✅(修复前返回首项)
  • firstNItems(-1)[] ✅(修复前返回首项)
  • firstNItems(2/3/10) 等正数行为完全不变 ✅(无回归)

影响范围

  • 仅改 applets/dde-apps/itemspage.cpp 一处,2 行新增。
  • count > 0 路径逻辑一字未改,无回归风险。
  • 全仓库(C++ / QML)检索 firstNItems 调用点为 0,方法仅声明未使用,生产路径无触发风险,属 API 契约级修复。
  • 不涉及 DBus 接口、Wayland 协议、DTK 控件、DConfig 配置,无需同步修改其他仓库。

审核

  • 标准-代码审核bot:✅ Approved(已确认早返逻辑正确、count > 0 路径无回归、无副作用、代码风格与既有写法一致)。

Ref: DDE-144

Summary by Sourcery

Ensure firstNItems honors empty-result semantics for non-positive item counts.

Bug Fixes:

  • Return an empty list from ItemsPage::firstNItems when the requested count is zero or negative, while preserving positive-count behavior.

Chores:

  • Update copyright years for the affected source files.

ItemsPage::firstNItems(count) appended an item before checking
result.count() >= count, so firstNItems(0) (and any count <= 0)
returned the first item instead of an empty list. Add an early
return for count <= 0 before the loop.

count > 0 behavior is unchanged (no regression). The method has no
current callers, so this is an API-contract fix only.

Ref: DDE-144
@deepin-ci-robot

Copy link
Copy Markdown

[APPROVALNOTIFIER] This PR is NOT APPROVED

This pull-request has been approved by: MyLeeJiEun

The full list of commands accepted by this bot can be found here.

Details Needs approval from an approver in each of these files:

Approvers can indicate their approval by writing /approve in a comment
Approvers can cancel approval by writing /approve cancel in a comment

@sourcery-ai

sourcery-ai Bot commented Aug 26, 2026

Copy link
Copy Markdown
Reviewer's guide (collapsed on small PRs)

Reviewer's Guide

Fixes the firstNItems contract for count <= 0 by returning an empty QStringList before iteration, while preserving behavior for all positive counts.

File-Level Changes

Change Details Files
Add an early return for non-positive requested counts so the method returns an empty list without entering the item-collection loop.
  • Handle zero and negative counts as empty-result requests.
  • Leave the existing positive-count iteration and boundary behavior unchanged.
applets/dde-apps/itemspage.cpp

Tips and commands

Interacting with Sourcery

  • Trigger a new review: Comment @sourcery-ai review on the pull request.
  • Continue discussions: Reply directly to Sourcery's review comments.
  • Generate a GitHub issue from a review comment: Ask Sourcery to create an
    issue from a review comment by replying to it. You can also reply to a
    review comment with @sourcery-ai issue to create an issue from it.
  • Generate a pull request title: Write @sourcery-ai anywhere in the pull
    request title to generate a title at any time. You can also comment
    @sourcery-ai title on the pull request to (re-)generate the title at any time.
  • Generate a pull request summary: Write @sourcery-ai summary anywhere in
    the pull request body to generate a PR summary at any time exactly where you
    want it. You can also comment @sourcery-ai summary on the pull request to
    (re-)generate the summary at any time.
  • Generate reviewer's guide: Comment @sourcery-ai guide on the pull
    request to (re-)generate the reviewer's guide at any time.
  • Resolve all Sourcery comments: Comment @sourcery-ai resolve on the
    pull request to resolve all Sourcery comments. Useful if you've already
    addressed all the comments and don't want to see them anymore.
  • Dismiss all Sourcery reviews: Comment @sourcery-ai dismiss on the pull
    request to dismiss all existing Sourcery reviews. Especially useful if you
    want to start fresh with a new review - don't forget to comment
    @sourcery-ai review to trigger a new review!

Customizing Your Experience

Access your dashboard to:

  • Enable or disable review features such as the Sourcery-generated pull request
    summary, the reviewer's guide, and others.
  • Change the review language.
  • Add, remove or edit custom review instructions.
  • Adjust other review settings.

Getting Help

@deepin-ci-robot

Copy link
Copy Markdown

Hi @MyLeeJiEun. Thanks for your PR.

I'm waiting for a linuxdeepin member to verify that this patch is reasonable to test. If it is, they should reply with /ok-to-test on its own line. Until that is done, I will not automatically test new commits in this PR, but the usual testing commands by org members will still work. Regular contributors should join the org to skip this step.

Once the patch is verified, the new status will be reflected by the ok-to-test label.

I understand the commands that are listed here.

Details

Instructions for interacting with me using PR comments are available here. If you have questions or suggestions related to my behavior, please file an issue against the kubernetes/test-infra repository.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants