Repository: nuttyartist/notes
File: src/nodetreeview.cpp (C++)

Code before:
    Q_D(NodeTreeView);
    d->delayedAutoScroll.stop();
    {
        auto index = indexAt(event->pos());
        if (index.isValid()) {
            auto itemType =
                    static_cast<NodeItem::Type>(index.data(NodeItem::Roles::ItemType).toInt());
            switch (itemType) {
            case NodeItem::Type::FolderItem: {
                auto rect = visualRect(index);
                auto iconRect = QRect(rect.x() + 5, rect.y() + (rect.height() - 20) / 2, 20, 20);
                if (iconRect.contains(event->pos())) {
                    if (isExpanded(index)) {
                        collapse(index);
                    } else {
                        expand(index);
                    }
                    return;
                }
                setCurrentIndexC(index);
                break;
            }
            case NodeItem::Type::TagItem: {
                auto oldIndexes = selectionModel()->selectedIndexes();
                for (const auto &ix : qAsConst(oldIndexes)) {
                    itemType =
                            static_cast<NodeItem::Type>(ix.data(NodeItem::Roles::ItemType).toInt());
                    if (itemType != NodeItem::Type::TagItem) {
                        setCurrentIndex(QModelIndex());
                        clearSelection();
                        break;
                    }
                }

                if (selectionModel()->isSelected(index)) {
                    m_needReleaseIndex = index;
                } else {
                    setCurrentIndex(index);
                    setSelectionMode(QAbstractItemView::MultiSelection);
                    selectionModel()->setCurrentIndex(index, QItemSelectionModel::SelectCurrent);
                }
                break;
            }
            case NodeItem::Type::AllNoteButton:
            case NodeItem::Type::TrashButton: {
                setCurrentIndexC(index);
                break;
            }
            default: {
                break;
            }

Review comment (suggestion):
I think this arbitrary variable re-use is confusing. This should be a new variable since it has little to do with the previous one. Maybe in the future, we end up changing the code such that the original `itemType` is still used beyond this scope, and then it may be changed to something random only in this case, which can be easy to miss.

If you really want to avoid shadowing warnings, the fix is to choose different names in my opinion.

Code after:
    Q_D(NodeTreeView);
    d->delayedAutoScroll.stop();
    {
        auto index = indexAt(event->pos());
        if (index.isValid()) {
            auto itemType =
                    static_cast<NodeItem::Type>(index.data(NodeItem::Roles::ItemType).toInt());
            switch (itemType) {
            case NodeItem::Type::FolderItem: {
                auto rect = visualRect(index);
                auto iconRect = QRect(rect.x() + 5, rect.y() + (rect.height() - 20) / 2, 20, 20);
                if (iconRect.contains(event->pos())) {
                    if (isExpanded(index)) {
                        collapse(index);
                    } else {
                        expand(index);
                    }
                    return;
                }
                setCurrentIndexC(index);
                break;
            }
            case NodeItem::Type::TagItem: {
                auto oldIndexes = selectionModel()->selectedIndexes();
                for (const auto &ix : qAsConst(oldIndexes)) {
                    auto selectedItemType =
                            static_cast<NodeItem::Type>(ix.data(NodeItem::Roles::ItemType).toInt());
                    if (selectedItemType != NodeItem::Type::TagItem) {
                        setCurrentIndex(QModelIndex());
                        clearSelection();
                        break;
                    }
                }

                if (selectionModel()->isSelected(index)) {
                    m_needReleaseIndex = index;
                } else {
                    setCurrentIndex(index);
                    setSelectionMode(QAbstractItemView::MultiSelection);
                    selectionModel()->setCurrentIndex(index, QItemSelectionModel::SelectCurrent);
                }
                break;
            }
            case NodeItem::Type::AllNoteButton:
            case NodeItem::Type::TrashButton: {
                setCurrentIndexC(index);
                break;
            }
            default: {
                break;
            }