Loose-Info.com
Last Update 2021/09/24
TOP - 各種テスト - gcc - 警告関連のオプション - -Wunused-value

-Wunused-value
効果のない(使用されない)結果を計算することとなるステートメントに関して警告を出力

テスト概要

オプション無し、-Wunused-value、-Wallの各オプションを使用した際の警告出力例

実行環境

GCC-8.2.0
GNU C Library 2.28
GNU Binutils 2.31.1


コード例・出力内容中の表記

・実行例中の太字表記部分は、コマンドなどの入力された文字列を示します。
・「」や「...」の着色省略表記は、 実際のソースコードや出力内容などを省略加工した部分を示します。

使用ファイル


sample.c
#include <stdio.h> int main(void) { int i = 1; int j[5] = {0, 1, 2, 3, 4}; /* 効果のない(使用されない)結果を計算することとなるステートメント */ /* -Wunused-valueオプションによる警告有り */ sizeof(int); i + 1; j[i]; (i < j[i]) + 1; /* 上記ステートメントをvoidでキャスト */ /* -Wunused-valueオプションによる警告無し */ (void)sizeof(int); (void)(i + 1); (void)j[i]; (void)((i < j[i]) + 1); printf("sample\n"); return 0; }

動作テスト

オプション無しでコンパイルを実行
$ gcc sample.c エラー・警告無し $

-Wunused-valueオプションを指定してコンパイルを実行
$ gcc -Wunused-value sample.c sample.c: In function ‘main’: sample.c:10:2: warning: statement with no effect [-Wunused-value] sizeof(int); ^~~~~~ sample.c:11:4: warning: statement with no effect [-Wunused-value] i + 1; ~~^~~ sample.c:12:3: warning: statement with no effect [-Wunused-value] j[i]; ~^~~ sample.c:13:13: warning: statement with no effect [-Wunused-value] (i < j[i]) + 1; ~~~~~~~~~~~^~~ $ 全て効果のないステートメントに関する警告

-Wallオプションを指定してコンパイルを実行
$ gcc -Wall sample.c sample.c: In function ‘main’: sample.c:10:2: warning: statement with no effect [-Wunused-value] sizeof(int); ^~~~~~ sample.c:11:4: warning: statement with no effect [-Wunused-value] i + 1; ~~^~~ sample.c:12:3: warning: statement with no effect [-Wunused-value] j[i]; ~^~~ sample.c:13:13: warning: statement with no effect [-Wunused-value] (i < j[i]) + 1; ~~~~~~~~~~~^~~ $ -Wunused-valueオプションは-Wallでも有効となる