メインコンテンツ

MISRA C:2012 Rule 5.2

Identifiers declared in the same scope and name space shall be distinct

説明

ルール定義

Identifiers declared in the same scope and name space shall be distinct 1 .

根拠

2 つの長い名前がほぼ同一で、違いが名前の後半部分のみである場合、それらを間違いやすくなります。コードの可読性は低下します。

Polyspace 実装

Polyspace® は最初の 63 文字の間に相違点があれば 2 つの名前を別個であると見なします。C90 では、相違点は最初の 31 文字の間に存在しなければなりません。C90 ルール チェックを使用するには、オプション [C 標準バージョン] (-c-version) で値 c90 を使用します。比較する文字数は、オプション -code-behavior-specifications を使用して変更できます。-code-behavior-specifications を参照してください。

レポート内の追加のメッセージ

Identifier XX has same significant characters as identifier YY.

トラブルシューティング

ルール違反を想定していてもその違反が表示されない場合、コーディング規約違反が想定どおりに表示されない理由の診断を参照します。

すべて展開する


extern int engine_exhaust_gas_temperature_raw;
static int engine_exhaust_gas_temperature_scaled;      /* Non-compliant */

extern double raw_engine_exhaust_gas_temperature;
static double scaled_engine_exhaust_gas_temperature;  /* Compliant */

void func ( void )
{
  /* Not in the same scope */
  int engine_exhaust_gas_temperature_local;            /* Compliant */
}

この例では、識別子 engine_exhaust_gas_temperature_scaled の 31 文字が、先行する識別子 engine_exhaust_gas_temperature_raw の文字と同じです。

2 つの識別子の 31 文字が同じであっても、スコープが異なる場合、このルールは適用されません。たとえば、engine_exhaust_gas_temperature_local には engine_exhaust_gas_temperature_raw と同じ 31 文字がありますが、スコープが異なります。


extern int engine_xxx_xxxxxxxxx_xxxxxxxxx_xxxxxxxxx_xxxxxxxxx_xxxxxxxxx_x_raw;
static int engine_xxx_xxxxxxxxx_xxxxxxxxx_xxxxxxxxx_xxxxxxxxx_xxxxxxxxx_x_scale; /* Non-compliant */

    
extern int engine_gas_xxxxxxxxx_xxxxxxxxx_xxxxxxxxx_xxxxxxxxx_xxxxxxxxx__raw;
static int engine_gas_xxxxxxxxx_xxxxxxxxx_xxxxxxxxx_xxxxxxxxx_xxxxxxxxx__scale; /* Compliant */

void func ( void )
{
/* Not in the same scope */
    int engine_xxx_xxxxxxxxx_xxxxxxxxx_xxxxxxxxx_xxxxxxxxx_xxxxxxxxx_x_local; /* Compliant */
}

この例では、識別子 engine_xxx_xxxxxxxxx_xxxxxxxxx_xxxxxxxxx_xxxxxxxxx_xxxxxxxxx_x_scale の 63 文字が、先行する識別子 engine_xxx_xxxxxxxxx_xxxxxxxxx_xxxxxxxxx_xxxxxxxxx_xxxxxxxxx_x_raw の文字と同じです。

チェック情報

グループ: 識別子
カテゴリ: 必要
AGC カテゴリ: 必要

バージョン履歴

R2014b で導入


1 All MISRA coding rules and directives are © Copyright The MISRA Consortium Limited 2021.

The MISRA coding standards referenced in the Polyspace Bug Finder™ documentation are from the following MISRA standards:

  • MISRA C:2004

  • MISRA C:2012

  • MISRA C:2023

  • MISRA C++:2008

  • MISRA C++:2023

MISRA and MISRA C are registered trademarks of The MISRA Consortium Limited 2021.