Encadenamiento opcional redundante
Esta categoría de advertencia se escribe [redundant-optional-chaining] por qmllint.
Encadenamiento opcional redundante
¿Qué ocurre?
Algunas búsquedas utilizan encadenamiento opcional cuando no es necesario. Esto puede ocurrir cuando se buscan valores enum o cuando se realiza una búsqueda en una base que no puede ser null o undefined.
¿Por qué es malo?
Una búsqueda opcional necesita realizar una comprobación en tiempo de ejecución que una búsqueda normal no realiza. No siempre se puede determinar que estas instrucciones adicionales son redundantes y la herramienta las optimiza. Entonces añaden un coste extra de rendimiento en tiempo de ejecución e hinchan el programa innecesariamente.
Además, la advertencia puede indicar que la búsqueda opcional se ha realizado en la base incorrecta de la cadena. Véase un ejemplo más concreto en la sección siguiente.
Ejemplo
// 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 }
Para corregir estas advertencias, sustituya las búsquedas opcionales redundantes por otras no opcionales:
© 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.