主要内容

本页翻译不是最新的。点击此处可查看最新英文版本。

MISRA C:2012 Rule 2.4

A project should not contain unused tag declarations

描述

默认 Polyspace® as You Code 分析中反激活了此检查项。请参阅Checkers Deactivated in Polyspace as You Code Analysis (Polyspace Access)

规则定义

A project should not contain unused tag declarations 1 .

理由

如果声明了一个标记但未使用,则评审不知道该标记是冗余的还是错误地未使用。

报告中的补充消息

工程不应包含未使用的标记声明:标记 tag_name 未使用。

故障排除

如果您预计存在违规,但未看到该违规,请参阅Diagnose Why Coding Standard Violations Do Not Appear as Expected

示例

全部展开

void unusedTag ( void )
{
    enum state1 { S_init, S_run, S_sleep };	/* Non-compliant  */			
}

void usedTag ( void )
{
    enum state2 { S_init, S_run, S_sleep };	/* Compliant  */			
    enum state2 my_State = S_init;
}

在此示例的函数 unusedTag 中,标记 state1 被定义但未使用。因此,违反了该规则。

typedef struct record_t		/* Non-compliant  */	
{
    unsigned short key;
    unsigned short val;
} record1_t;


typedef struct			   /* Compliant */						
{
    unsigned short key;
    unsigned short val;
} record2_t;

record1_t myRecord1_t;
record2_t myRecord2_t;

在此示例中,标记 record_t 仅出现在 record1_ttypedef 中。在转换单元的其余部分,使用了类型 record1_t。因此,违反了该规则。

检查信息

组:未使用的代码
类别:建议
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.