Skip to contentSkip to conversation

selftests: Suppress unused variable warning

PATCH RESEND In review Chen Linxuan 3 participants 8 messages Jun 10, 2025 – Jun 20, 2025 Kselftest

lore
Share & tools
lore
#0 lore raw

PATCH RESEND 1/1 selftests: Suppress unused variable warning

When running make kselftest, the following compilation warning was encountered:

mount-notify_test.c: In function ‘fanotify_rmdir’:
mount-notify_test.c:490:17: warning: ignoring return value of ‘chdir’ declared with attribute ‘warn_unused_result’ [-Wunused-result]

  490 |                 chdir("/");
      |                 ^~~~~~~~~~

This patch addresses the warning by
explicitly suppressing the unused result of the chdir function.

---
 .../selftests/filesystems/mount-notify/mount-notify_test.c    | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)
tools/testing/selftests/filesystems/mount-notify/mount-notify_test.c +3 -1
index 63ce708d93ed0..34afe27b7978f 100644
@@ -465,7 +465,9 @@ TEST_F(fanotify, rmdir)
 	ASSERT_GE(ret, 0);
 
 	if (ret == 0) {
-		chdir("/");
+		// Suppress -Wunused-result
+		// Ref: https://gcc.gnu.org/bugzilla/show_bug.cgi?id=66425#c34
+		(void) !chdir("/");
 		unshare(CLONE_NEWNS);
 		mount("", "/", NULL, MS_REC|MS_PRIVATE, NULL);
 		umount2("/a", MNT_DETACH);
Signature · 1 line
2.43.0
maintainer
#1 lore raw
Quoted 20 lines · Chen Linxuan

When running make kselftest, the following compilation warning was encountered:

mount-notify_test.c: In function ‘fanotify_rmdir’:
mount-notify_test.c:490:17: warning: ignoring return value of ‘chdir’ declared with attribute ‘warn_unused_result’ [-Wunused-result]

   490 |                 chdir("/");
       |                 ^~~~~~~~~~

This patch addresses the warning by
explicitly suppressing the unused result of the chdir function.

---
  .../selftests/filesystems/mount-notify/mount-notify_test.c    | 4 +++-
  1 file changed, 3 insertions(+), 1 deletion(-)
tools/testing/selftests/filesystems/mount-notify/mount-notify_test.c +0 -0
index 63ce708d93ed0..34afe27b7978f 100644

tools/testing/selftests/filesystems/mount-notify/mount-notify_test.c TEST_F(fanotify, rmdir) · line 470

Quoted 7 lines
tools/testing/selftests/filesystems/mount-notify/mount-notify_test.c +3 -1
@@ -465,7 +465,9 @@ TEST_F(fanotify, rmdir)
  	ASSERT_GE(ret, 0);
 
  	if (ret == 0) {
-		chdir("/");
+		// Suppress -Wunused-result
+		// Ref: https://gcc.gnu.org/bugzilla/show_bug.cgi?id=66425#c34
+		(void) !chdir("/");

Why not fix the problem the right way by checking the return value. Suppressing the error isn't useful.

tools/testing/selftests/filesystems/mount-notify/mount-notify_test.c TEST_F(fanotify, rmdir) · line 473

Quoted 3 lines
tools/testing/selftests/filesystems/mount-notify/mount-notify_test.c +0 -0
  		unshare(CLONE_NEWNS);
  		mount("", "/", NULL, MS_REC|MS_PRIVATE, NULL);
  		umount2("/a", MNT_DETACH);

thanks,
-- Shuah

#2 lore raw
Quoted 24 lines · Shuah Khan
Quoted 22 lines · Chen Linxuan

When running make kselftest, the following compilation warning was encountered:

mount-notify_test.c: In function ‘fanotify_rmdir’:
mount-notify_test.c:490:17: warning: ignoring return value of ‘chdir’ declared with attribute ‘warn_unused_result’ [-Wunused-result]

   490 |                 chdir("/");
       |                 ^~~~~~~~~~

This patch addresses the warning by
explicitly suppressing the unused result of the chdir function.

---
  .../selftests/filesystems/mount-notify/mount-notify_test.c    | 4 +++-
  1 file changed, 3 insertions(+), 1 deletion(-)
tools/testing/selftests/filesystems/mount-notify/mount-notify_test.c +0 -0
index 63ce708d93ed0..34afe27b7978f 100644
@@ -465,7 +465,9 @@ TEST_F(fanotify, rmdir)
      ASSERT_GE(ret, 0);
 
Quoted 8 lines

tools/testing/selftests/filesystems/mount-notify/mount-notify_test.c TEST_F(fanotify, rmdir) · line 470

Quoted 5 lines
tools/testing/selftests/filesystems/mount-notify/mount-notify_test.c +3 -1
      if (ret == 0) {
-             chdir("/");
+             // Suppress -Wunused-result
+             // Ref: https://gcc.gnu.org/bugzilla/show_bug.cgi?id=66425#c34
+             (void) !chdir("/");

Why not fix the problem the right way by checking the return value. Suppressing the error isn't useful.

The code is already handling cleanup in error cases,
and I don't think checking the result of chdir would be useful here.

Quoted 10 lines

tools/testing/selftests/filesystems/mount-notify/mount-notify_test.c TEST_F(fanotify, rmdir) · line 473

Quoted 5 lines
tools/testing/selftests/filesystems/mount-notify/mount-notify_test.c +0 -0
              unshare(CLONE_NEWNS);
              mount("", "/", NULL, MS_REC|MS_PRIVATE, NULL);
              umount2("/a", MNT_DETACH);

thanks,
-- Shuah

maintainer
#3 lore raw
Quoted 30 lines · Chen Linxuan
Quoted 29 lines · Shuah Khan
Quoted 27 lines · Chen Linxuan

When running make kselftest, the following compilation warning was encountered:

mount-notify_test.c: In function ‘fanotify_rmdir’:
mount-notify_test.c:490:17: warning: ignoring return value of ‘chdir’ declared with attribute ‘warn_unused_result’ [-Wunused-result]

    490 |                 chdir("/");
        |                 ^~~~~~~~~~

This patch addresses the warning by
explicitly suppressing the unused result of the chdir function.

---
   .../selftests/filesystems/mount-notify/mount-notify_test.c    | 4 +++-
   1 file changed, 3 insertions(+), 1 deletion(-)
tools/testing/selftests/filesystems/mount-notify/mount-notify_test.c +3 -1
index 63ce708d93ed0..34afe27b7978f 100644
@@ -465,7 +465,9 @@ TEST_F(fanotify, rmdir)
       ASSERT_GE(ret, 0);
 
       if (ret == 0) {
-             chdir("/");
+             // Suppress -Wunused-result
+             // Ref: https://gcc.gnu.org/bugzilla/show_bug.cgi?id=66425#c34
+             (void) !chdir("/");
Quoted 5 lines
Quoted 2 lines
Quoted 1 line
Quoted 1 line

Why not fix the problem the right way by checking the return value.

Suppressing the error isn't useful.

The code is already handling cleanup in error cases,
and I don't think checking the result of chdir would be useful here.

We check for chdir() in several tools in the kernel. Add a check for it instead of suppressing the [-Wunused-result] - suppressing doesn't do any good.

thanks,
-- Shuah

#4 lore raw
Quoted 3 lines · Shuah Khan
Quoted 2 lines · Chen Linxuan
Quoted 1 line · Shuah Khan

On 6/9/25 20:07, Chen Linxuan wrote:

...

Quoted 9 lines
Quoted 9 lines
Quoted 9 lines
Quoted 9 lines
tools/testing/selftests/filesystems/mount-notify/ mount-notify_test.c renamed from tools/testing/selftests/filesystems/mount-notify/mount- notify_test.c +0 -1
index 63ce708d93ed0..34afe27b7978f 100644
--- a/tools/testing/selftests/filesystems/mount-notify/mount- 

notify_test.c
+++ b/tools/testing/selftests/filesystems/mount-notify/mount- notify_test.c

tools/testing/selftests/filesystems/mount-notify/ mount-notify_test.c renamed from tools/testing/selftests/filesystems/mount-notify/mount- notify_test.c +0 -0
@@ -465,7 +465,9 @@ TEST_F(fanotify, rmdir)
        ASSERT_GE(ret, 0);
 
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
tools/testing/selftests/filesystems/mount-notify/ mount-notify_test.c renamed from tools/testing/selftests/filesystems/mount-notify/mount- notify_test.c +2 -1
        if (ret == 0) {
-             chdir("/");
+             // Suppress -Wunused-result
+             // Ref: https://gcc.gnu.org/bugzilla/show_bug.cgi? 

id=66425#c34
+             (void) !chdir("/");

This is quite ugly. :)

Quoted 6 lines
Quoted 5 lines
Quoted 2 lines
Quoted 1 line
Quoted 1 line

Why not fix the problem the right way by checking the return value.

Suppressing the error isn't useful.

The code is already handling cleanup in error cases,
and I don't think checking the result of chdir would be useful here.

Why not just fail with the appropriate test result, if chdir() fails here, instead of making a bit of a mess with odd void casts to a negated return value, and a reference to a compiler bug report?

Really, Shuah is putting you on the right path here.

Quoted 8 lines

We check for chdir() in several tools in the kernel. Add a check for it instead of suppressing the [-Wunused-result] - suppressing doesn't do any good.

thanks,
-- Shuah

thanks,

Signature · 1 line
John Hubbard
maintainer
#5 lore raw
Quoted 27 lines · John Hubbard
Quoted 3 lines · Shuah Khan
Quoted 2 lines · Chen Linxuan
Quoted 1 line · Shuah Khan

On 6/9/25 20:07, Chen Linxuan wrote:

...

Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
tools/testing/selftests/filesystems/mount-notify/mount- notify_test.c +0 -0
index 63ce708d93ed0..34afe27b7978f 100644
@@ -465,7 +465,9 @@ TEST_F(fanotify, rmdir)
        ASSERT_GE(ret, 0);
 
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines

tools/testing/selftests/filesystems/mount-notify/mount- notify_test.c TEST_F(fanotify, rmdir) · line 470

Quoted 5 lines
tools/testing/selftests/filesystems/mount-notify/mount- notify_test.c +3 -1
        if (ret == 0) {
-             chdir("/");
+             // Suppress -Wunused-result
+             // Ref: https://gcc.gnu.org/bugzilla/show_bug.cgi? id=66425#c34
+             (void) !chdir("/");

This is quite ugly. :)

Quoted 6 lines
Quoted 5 lines
Quoted 2 lines
Quoted 1 line
Quoted 1 line

Why not fix the problem the right way by checking the return value.

Suppressing the error isn't useful.

The code is already handling cleanup in error cases,
and I don't think checking the result of chdir would be useful here.

Quoted 5 lines

Why not just fail with the appropriate test result, if chdir() fails here, instead of making a bit of a mess with odd void casts to a negated return value, and a reference to a compiler bug report?

Really, Shuah is putting you on the right path here.

Ha. I didn't ask to suppress the error with the cast. I asked to check the return and fail.

Quoted 4 lines
Quoted 4 lines

We check for chdir() in several tools in the kernel. Add a check for it instead of suppressing the [-Wunused-result] - suppressing doesn't do any good.

This is what I said.

thanks,
-- Shuah

maintainer
#6 lore raw
Quoted 28 lines · Shuah Khan
Quoted 27 lines · John Hubbard
Quoted 3 lines · Shuah Khan
Quoted 2 lines · Chen Linxuan
Quoted 1 line · Shuah Khan

On 6/9/25 20:07, Chen Linxuan wrote:

...

Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
tools/testing/selftests/filesystems/mount-notify/mount- notify_test.c +0 -0
index 63ce708d93ed0..34afe27b7978f 100644
@@ -465,7 +465,9 @@ TEST_F(fanotify, rmdir)
        ASSERT_GE(ret, 0);
 
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines

tools/testing/selftests/filesystems/mount-notify/mount- notify_test.c TEST_F(fanotify, rmdir) · line 470

Quoted 5 lines
tools/testing/selftests/filesystems/mount-notify/mount- notify_test.c +3 -1
        if (ret == 0) {
-             chdir("/");
+             // Suppress -Wunused-result
+             // Ref: https://gcc.gnu.org/bugzilla/show_bug.cgi? id=66425#c34
+             (void) !chdir("/");

This is quite ugly. :)

Quoted 6 lines
Quoted 5 lines
Quoted 2 lines
Quoted 1 line
Quoted 1 line

Why not fix the problem the right way by checking the return value.

Suppressing the error isn't useful.

The code is already handling cleanup in error cases,
and I don't think checking the result of chdir would be useful here.

Quoted 10 lines
Quoted 5 lines

Why not just fail with the appropriate test result, if chdir() fails here, instead of making a bit of a mess with odd void casts to a negated return value, and a reference to a compiler bug report?

Really, Shuah is putting you on the right path here.

Ha. I didn't ask to suppress the error with the cast. I asked to check the return and fail.

Quoted 1 line

Sorry John. I didn't read it correctly the first time. I probably go get more coffee. :)

thanks,
-- Shuah

#7 lore raw
In reply to Shuah Khan
Quoted 14 lines · Shuah Khan
Quoted 12 lines · John Hubbard
Quoted 3 lines · Shuah Khan
Quoted 2 lines · Chen Linxuan
Quoted 1 line · Shuah Khan

On 6/9/25 20:07, Chen Linxuan wrote:

...

Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
tools/testing/selftests/filesystems/mount-notify/mount- notify_test.c +0 -0
index 63ce708d93ed0..34afe27b7978f 100644
@@ -465,7 +465,9 @@ TEST_F(fanotify, rmdir)
       ASSERT_GE(ret, 0);
 
Quoted 7 lines
Quoted 7 lines
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines

tools/testing/selftests/filesystems/mount-notify/mount- notify_test.c TEST_F(fanotify, rmdir) · line 470

Quoted 5 lines
tools/testing/selftests/filesystems/mount-notify/mount- notify_test.c +3 -1
       if (ret == 0) {
-             chdir("/");
+             // Suppress -Wunused-result
+             // Ref: https://gcc.gnu.org/bugzilla/show_bug.cgi? id=66425#c34
+             (void) !chdir("/");

This is quite ugly. :)

I agree with you. :)

Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
Quoted 3 lines
Quoted 2 lines
Quoted 2 lines

Why not fix the problem the right way by checking the return value.

Suppressing the error isn't useful.

The code is already handling cleanup in error cases,
and I don't think checking the result of chdir would be useful here.

I think I was mistaken earlier. Here we are in the child process after a fork, not handling an error case.
I think simply calling exit(-1) here when chdir failed should be enough to make the test fail in the parent process.
Maybe we should do the same for other similar calls as well. I will send a v2 soon.

Quoted 22 lines
Quoted 7 lines
Quoted 1 line

Why not just fail with the appropriate test result, if chdir() fails here, instead of making a bit of a mess with odd void casts to a negated return value, and a reference to a compiler bug report?

Really, Shuah is putting you on the right path here.

Ha. I didn't ask to suppress the error with the cast. I asked to check the return and fail.

Quoted 4 lines
Quoted 4 lines

We check for chdir() in several tools in the kernel. Add a check for it instead of suppressing the [-Wunused-result] - suppressing doesn't do any good.

This is what I said.

thanks,
-- Shuah