Skip to contentSkip to conversation

qobject: Use qobject_to() instead of type cast

PATCH Applied Markus Armbruster 3 participants 4 messages Apr 26, 2018 – May 2, 2018

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 qobject: Use qobject_to() instead of type cast

The proper way to convert from (abstract) QObject to a (concrete) subtype is qobject_to(). Look for offenders that type cast instead:

    $ git-grep '(Q[A-Z][a-z]* \*)'
    hmp.c:    qmp_device_add((QDict *)qdict, NULL, &err);
    include/qapi/qmp/qobject.h:        return (QObject *)obj;
    qobject/qobject.c:static void (*qdestroy[QTYPE__MAX])(QObject *) = {
    tests/check-qdict.c:    dst = (QDict *)qdict_crumple(src, &error_abort);

The first two cast away const, the third isn't a type cast. Fix the fourth.

---
 tests/check-qdict.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
tests/check-qdict.c +1 -1
index a0e0454684..eba5d3528e 100644
@@ -657,7 +657,7 @@ static void qdict_crumple_test_empty(void)
 
     src = qdict_new();
 
-    dst = (QDict *)qdict_crumple(src, &error_abort);
+    dst = qobject_to(QDict, qdict_crumple(src, &error_abort));
 
     g_assert_cmpint(qdict_size(dst), ==, 0);
 
Signature · 1 line
2.13.6
Reviewed-by on v1 1/1
#1 lore raw
Quoted 9 lines · Markus Armbruster

The proper way to convert from (abstract) QObject to a (concrete) subtype is qobject_to(). Look for offenders that type cast instead:

    $ git-grep '(Q[A-Z][a-z]* \*)'
    hmp.c:    qmp_device_add((QDict *)qdict, NULL, &err);
    include/qapi/qmp/qobject.h:        return (QObject *)obj;
    qobject/qobject.c:static void (*qdestroy[QTYPE__MAX])(QObject *) = {
    tests/check-qdict.c:    dst = (QDict *)qdict_crumple(src, &error_abort);
Quoted 7 lines

The first two cast away const, the third isn't a type cast. Fix the fourth.

---
 tests/check-qdict.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
Signature · 3 lines
Eric Blake, Principal Software Engineer
Red Hat, Inc.           +1-919-301-3266
Virtualization:  qemu.org | libvirt.org
Reviewed-by on v1 1/1
#2 lore raw
In reply to Markus Armbruster
Quoted 9 lines · Markus Armbruster

The proper way to convert from (abstract) QObject to a (concrete) subtype is qobject_to(). Look for offenders that type cast instead:

    $ git-grep '(Q[A-Z][a-z]* \*)'
    hmp.c:    qmp_device_add((QDict *)qdict, NULL, &err);
    include/qapi/qmp/qobject.h:        return (QObject *)obj;
    qobject/qobject.c:static void (*qdestroy[QTYPE__MAX])(QObject *) = {
    tests/check-qdict.c:    dst = (QDict *)qdict_crumple(src, &error_abort);
Quoted 4 lines

The first two cast away const, the third isn't a type cast. Fix the fourth.

Quoted 20 lines
---
 tests/check-qdict.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
tests/check-qdict.c +1 -2
index a0e0454684..eba5d3528e 100644
@@ -657,7 +657,7 @@ static void qdict_crumple_test_empty(void)
 
     src = qdict_new();
 
-    dst = (QDict *)qdict_crumple(src, &error_abort);
+    dst = qobject_to(QDict, qdict_crumple(src, &error_abort));
 
     g_assert_cmpint(qdict_size(dst), ==, 0);
 
--

2.13.6