Skip to contentSkip to conversation

simplify container_of_const()

PATCH In review Alexey Dobriyan 1 participant 2 messages Jul 19, 2024

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 simplify container_of_const()

* add necessary parenthesis so that

container_of_const((expr1, expr2), T, member)

compiles without warnings,

* delete unnecessary parenthesis,

* delete unnecessary cast:

container_of(ptr, T, member)

has type "T*" and doesn't need any more casts.

---

 include/linux/container_of.h |    6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)
include/linux/container_of.h +3 -3
@@ -30,9 +30,9 @@
  * @member:		the name of the member within the struct.
  */
 #define container_of_const(ptr, type, member)				\
-	_Generic(ptr,							\
-		const typeof(*(ptr)) *: ((const type *)container_of(ptr, type, member)),\
-		default: ((type *)container_of(ptr, type, member))	\
+	_Generic((ptr),							\
+		const typeof(*(ptr)) *: (const type *)container_of(ptr, type, member),\
+		default: container_of(ptr, type, member)		\
 	)
 
 #endif	/* _LINUX_CONTAINER_OF_H */
#1 lore raw
Quoted 5 lines · Alexey Dobriyan

* add necessary parenthesis so that

container_of_const((expr1, expr2), T, member)

compiles without warnings,

This part is bogus, I'm not sure why I wrote this.

include/linux/container_of.h line 33

Quoted 10 lines
include/linux/container_of.h +1 -3
@@ -30,9 +30,9 @@
  * @member:		the name of the member within the struct.
  */
 #define container_of_const(ptr, type, member)				\
-	_Generic(ptr,							\
-		const typeof(*(ptr)) *: ((const type *)container_of(ptr, type, member)),\
-		default: ((type *)container_of(ptr, type, member))	\
+	_Generic((ptr),							\

someone could sneak in additional clause...

Quoted 2 lines
include/linux/container_of.h +2 -0
+		const typeof(*(ptr)) *: (const type *)container_of(ptr, type, member),\
+		default: container_of(ptr, type, member)		\