ALSA: kunit: Fix sparse warnings
authorTakashi Iwai <tiwai@suse.de>
Tue, 27 Feb 2024 10:49:12 +0000 (11:49 +0100)
committerTakashi Iwai <tiwai@suse.de>
Wed, 28 Feb 2024 14:00:18 +0000 (15:00 +0100)
There were a few sparse warnings about the cast of strong-typed
snd_pcm_format_t.  Fix them with cast with __force.

For spreading the ugly mess, put them in the definitions
WRONG_FORMAT_1 and WRONG_FORMAT_2 and use them in the callers.

Fixes: 3e39acf56ede ("ALSA: core: Add sound core KUnit test")
Reported-by: kernel test robot <lkp@intel.com>
Closes: https://lore.kernel.org/r/202402270303.PmvmQrJV-lkp@intel.com
Link: https://lore.kernel.org/r/20240227104912.18921-1-tiwai@suse.de
Signed-off-by: Takashi Iwai <tiwai@suse.de>
sound/core/sound_kunit.c

index 4212c4a..eb90f62 100644 (file)
@@ -17,7 +17,8 @@
        .name = #fmt,                                                           \
 }
 
-#define WRONG_FORMAT (SNDRV_PCM_FORMAT_LAST + 1)
+#define WRONG_FORMAT_1 (__force snd_pcm_format_t)((__force int)SNDRV_PCM_FORMAT_LAST + 1)
+#define WRONG_FORMAT_2 (__force snd_pcm_format_t)-1
 
 #define VALID_NAME "ValidName"
 #define NAME_W_SPEC_CHARS "In%v@1id name"
@@ -104,8 +105,8 @@ static void test_phys_format_size(struct kunit *test)
                                valid_fmt[i].physical_bits);
        }
 
-       KUNIT_EXPECT_EQ(test, snd_pcm_format_physical_width(WRONG_FORMAT), -EINVAL);
-       KUNIT_EXPECT_EQ(test, snd_pcm_format_physical_width(-1), -EINVAL);
+       KUNIT_EXPECT_EQ(test, snd_pcm_format_physical_width(WRONG_FORMAT_1), -EINVAL);
+       KUNIT_EXPECT_EQ(test, snd_pcm_format_physical_width(WRONG_FORMAT_2), -EINVAL);
 }
 
 static void test_format_width(struct kunit *test)
@@ -117,8 +118,8 @@ static void test_format_width(struct kunit *test)
                                valid_fmt[i].width);
        }
 
-       KUNIT_EXPECT_EQ(test, snd_pcm_format_width(WRONG_FORMAT), -EINVAL);
-       KUNIT_EXPECT_EQ(test, snd_pcm_format_width(-1), -EINVAL);
+       KUNIT_EXPECT_EQ(test, snd_pcm_format_width(WRONG_FORMAT_1), -EINVAL);
+       KUNIT_EXPECT_EQ(test, snd_pcm_format_width(WRONG_FORMAT_2), -EINVAL);
 }
 
 static void test_format_signed(struct kunit *test)
@@ -132,8 +133,8 @@ static void test_format_signed(struct kunit *test)
                                valid_fmt[i].sd < 0 ? -EINVAL : 1 - valid_fmt[i].sd);
        }
 
-       KUNIT_EXPECT_EQ(test, snd_pcm_format_width(WRONG_FORMAT), -EINVAL);
-       KUNIT_EXPECT_EQ(test, snd_pcm_format_width(-1), -EINVAL);
+       KUNIT_EXPECT_EQ(test, snd_pcm_format_width(WRONG_FORMAT_1), -EINVAL);
+       KUNIT_EXPECT_EQ(test, snd_pcm_format_width(WRONG_FORMAT_2), -EINVAL);
 }
 
 static void test_format_endianness(struct kunit *test)
@@ -147,10 +148,10 @@ static void test_format_endianness(struct kunit *test)
                                valid_fmt[i].le < 0 ? -EINVAL : 1 - valid_fmt[i].le);
        }
 
-       KUNIT_EXPECT_EQ(test, snd_pcm_format_little_endian(WRONG_FORMAT), -EINVAL);
-       KUNIT_EXPECT_EQ(test, snd_pcm_format_little_endian(-1), -EINVAL);
-       KUNIT_EXPECT_EQ(test, snd_pcm_format_big_endian(WRONG_FORMAT), -EINVAL);
-       KUNIT_EXPECT_EQ(test, snd_pcm_format_big_endian(-1), -EINVAL);
+       KUNIT_EXPECT_EQ(test, snd_pcm_format_little_endian(WRONG_FORMAT_1), -EINVAL);
+       KUNIT_EXPECT_EQ(test, snd_pcm_format_little_endian(WRONG_FORMAT_2), -EINVAL);
+       KUNIT_EXPECT_EQ(test, snd_pcm_format_big_endian(WRONG_FORMAT_1), -EINVAL);
+       KUNIT_EXPECT_EQ(test, snd_pcm_format_big_endian(WRONG_FORMAT_2), -EINVAL);
 }
 
 static void _test_fill_silence(struct kunit *test, struct snd_format_test_data *data,
@@ -177,7 +178,7 @@ static void test_format_fill_silence(struct kunit *test)
                        _test_fill_silence(test, &valid_fmt[j], buffer, buf_samples[i]);
        }
 
-       KUNIT_EXPECT_EQ(test, snd_pcm_format_set_silence(WRONG_FORMAT, buffer, 20), -EINVAL);
+       KUNIT_EXPECT_EQ(test, snd_pcm_format_set_silence(WRONG_FORMAT_1, buffer, 20), -EINVAL);
        KUNIT_EXPECT_EQ(test, snd_pcm_format_set_silence(SNDRV_PCM_FORMAT_LAST, buffer, 0), 0);
 }
 
@@ -272,8 +273,8 @@ static void test_pcm_format_name(struct kunit *test)
                KUNIT_EXPECT_STREQ(test, name, valid_fmt[i].name);
        }
 
-       KUNIT_ASSERT_STREQ(test, snd_pcm_format_name(WRONG_FORMAT), "Unknown");
-       KUNIT_ASSERT_STREQ(test, snd_pcm_format_name(-1), "Unknown");
+       KUNIT_ASSERT_STREQ(test, snd_pcm_format_name(WRONG_FORMAT_1), "Unknown");
+       KUNIT_ASSERT_STREQ(test, snd_pcm_format_name(WRONG_FORMAT_2), "Unknown");
 }
 
 static void test_card_add_component(struct kunit *test)