From 972b48ef24dc595de1a3d6a0da0fd4a3c1da1650 Mon Sep 17 00:00:00 2001 From: Christian Gagneraud Date: Sat, 25 Feb 2017 11:45:13 +1300 Subject: [PATCH] CppEditor: Use qFuzzyCompare when comparing member variable for qproperty refactoring For a line like Q_PROPERTY(qreal foo READ foo WRITE setFoo NOTIFY fooChanged) the generated setter will now use "if(qFuzzyCompare(m_foo, foo))" instead of "if (m_foo == foo)" for types that are supported by qFuzzyCompare (that is: qreal, double and float). A warning stating that "Floating point comparison needs context sanity check" is as well added to remind the user to check/fix the generated code. Change-Id: I8d274d5c072d107f0d04b1e153b5cc183e6317fc Reviewed-by: Nikolai Kosjar --- src/plugins/cppeditor/cppquickfixes.cpp | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) diff --git a/src/plugins/cppeditor/cppquickfixes.cpp b/src/plugins/cppeditor/cppquickfixes.cpp index 4c03ed4e704..07373ace5fb 100644 --- a/src/plugins/cppeditor/cppquickfixes.cpp +++ b/src/plugins/cppeditor/cppquickfixes.cpp @@ -195,6 +195,13 @@ inline bool isQtStringTranslation(const QByteArray &id) return id == "tr" || id == "trUtf8" || id == "translate" || id == "QT_TRANSLATE_NOOP"; } +inline bool isQtFuzzyComparable(const QString &typeName) +{ + return typeName == QLatin1String("double") + || typeName == QLatin1String("float") + || typeName == QLatin1String("qreal"); +} + Class *isMemberFunction(const LookupContext &context, Function *function) { QTC_ASSERT(function, return 0); @@ -4461,8 +4468,13 @@ public: if (m_signalName.isEmpty()) { setter << m_storageName << " = " << baseName << ";\n}\n"; } else { - setter << "if (" << m_storageName << " == " << baseName << ")\nreturn;\n\n" - << m_storageName << " = " << baseName << ";\nemit " << m_signalName + if (isQtFuzzyComparable(typeName)) { + setter << "qWarning(\"Floating point comparison needs context sanity check\");\n"; + setter << "if (qFuzzyCompare(" << m_storageName << ", " << baseName << "))\nreturn;\n\n"; + } + else + setter << "if (" << m_storageName << " == " << baseName << ")\nreturn;\n\n"; + setter << m_storageName << " = " << baseName << ";\nemit " << m_signalName << '(' << m_storageName << ");\n}\n"; } InsertionLocation setterLoc = locator.methodDeclarationInClass(file->fileName(), m_class, InsertionPointLocator::PublicSlot);