このページについて

冗長なオプションチェーン

この警告カテゴリは、qmllint では「[redundant-optional-chaining] 」と表記されます。

冗長なオプションチェーン

何が起きたのか?

一部のルックアップで、必要のない場面でオプションチェイニングが使用されています。これは、列挙型の値をルックアップする場合や、null またはundefined ではない基底クラスに対してルックアップを行う場合に発生する可能性があります。

なぜ問題なのか?

オプションのルックアップでは、通常のルックアップでは行われない実行時のチェックを行う必要があります。これらの余分な命令は、ツールによって常に冗長であると判断され、最適化によって削除されるとは限りません。その結果、実行時のパフォーマンスに余分な負荷がかかり、プログラムが不必要に肥大化してしまいます。

さらに、この警告は、チェーン内の誤ったベースに対してオプションのルックアップが行われた可能性を示唆している場合もあります。より具体的な例については、次のセクションを参照してください。

例

// Main.qml
import QtQml

QtObject {
    // Main will either be resolved and always work, or throw an exception or fail to compile
    enum E { A, B, C }
    property int i: Main?.A

    // A url cannot be null or undefined
    property url u: ""
    property string s: u?.toString()

    // Did you mean to make the second lookup optional?
    property int i: Safe?.Unsafe.i
}

これらの警告を修正するには、冗長なオプションのルックアップを非オプションのルックアップに置き換えてください:

// Main.qml
import QtQml

QtObject {
    enum E { A, B, C }
    property int i: Main.A

    property url u: ""
    property string s: u.toString()

    property int i: Safe.Unsafe?.i
}

© 2026 The Qt Company Ltd. Documentation contributions included herein are the copyrights of their respective owners. The documentation provided herein is licensed under the terms of the GNU Free Documentation License version 1.3 as published by the Free Software Foundation. Qt and respective logos are trademarks of The Qt Company Ltd. in Finland and/or other countries worldwide. All other trademarks are property of their respective owners.