冗長なオプショナル・チェイニング
この警告カテゴリーは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 }
これらの警告を修正するには、冗長なオプショナル・ルックアップをオプショナルでないものに置き換える:
© 2025 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.