Skip to content

Commit 03aa93d

Browse files
Merge pull request #21374 from Jefffrey/excuse-non-camel-case-repr-c
fix: don't fire `non_camel_case_types` lint for structs/enums marked with `repr(C)`
2 parents 2b4263b + a50a99a commit 03aa93d

4 files changed

Lines changed: 50 additions & 14 deletions

File tree

crates/hir-def/src/signatures.rs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -185,6 +185,9 @@ impl UnionSignature {
185185
bitflags! {
186186
#[derive(Debug, Copy, Clone, PartialEq, Eq)]
187187
pub struct EnumFlags: u8 {
188+
/// Indicates whether this enum has `#[repr]`.
189+
const HAS_REPR = 1 << 0;
190+
/// Indicates whether the enum has a `#[rustc_has_incoherent_inherent_impls]` attribute.
188191
const RUSTC_HAS_INCOHERENT_INHERENT_IMPLS = 1 << 1;
189192
}
190193
}
@@ -205,6 +208,9 @@ impl EnumSignature {
205208
if attrs.contains(AttrFlags::RUSTC_HAS_INCOHERENT_INHERENT_IMPLS) {
206209
flags |= EnumFlags::RUSTC_HAS_INCOHERENT_INHERENT_IMPLS;
207210
}
211+
if attrs.contains(AttrFlags::HAS_REPR) {
212+
flags |= EnumFlags::HAS_REPR;
213+
}
208214

209215
let InFile { file_id, value: source } = loc.source(db);
210216
let (store, generic_params, source_map) = lower_generic_params(
@@ -233,6 +239,11 @@ impl EnumSignature {
233239
_ => IntegerType::Pointer(true),
234240
}
235241
}
242+
243+
#[inline]
244+
pub fn repr(&self, db: &dyn DefDatabase, id: EnumId) -> Option<ReprOptions> {
245+
if self.flags.contains(EnumFlags::HAS_REPR) { AttrFlags::repr(db, id.into()) } else { None }
246+
}
236247
}
237248
bitflags::bitflags! {
238249
#[derive(Debug, Clone, Copy, Eq, PartialEq, Default)]

crates/hir-ty/src/diagnostics/decl_check.rs

Lines changed: 24 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -293,12 +293,18 @@ impl<'a> DeclValidator<'a> {
293293
fn validate_struct(&mut self, struct_id: StructId) {
294294
// Check the structure name.
295295
let data = self.db.struct_signature(struct_id);
296-
self.create_incorrect_case_diagnostic_for_item_name(
297-
struct_id,
298-
&data.name,
299-
CaseType::UpperCamelCase,
300-
IdentType::Structure,
301-
);
296+
297+
// rustc implementation excuses repr(C) since C structs predominantly don't
298+
// use camel case.
299+
let has_repr_c = data.repr(self.db, struct_id).is_some_and(|repr| repr.c());
300+
if !has_repr_c {
301+
self.create_incorrect_case_diagnostic_for_item_name(
302+
struct_id,
303+
&data.name,
304+
CaseType::UpperCamelCase,
305+
IdentType::Structure,
306+
);
307+
}
302308

303309
// Check the field names.
304310
self.validate_struct_fields(struct_id);
@@ -378,15 +384,20 @@ impl<'a> DeclValidator<'a> {
378384
}
379385

380386
fn validate_enum(&mut self, enum_id: EnumId) {
387+
// Check the enum name.
381388
let data = self.db.enum_signature(enum_id);
382389

383-
// Check the enum name.
384-
self.create_incorrect_case_diagnostic_for_item_name(
385-
enum_id,
386-
&data.name,
387-
CaseType::UpperCamelCase,
388-
IdentType::Enum,
389-
);
390+
// rustc implementation excuses repr(C) since C structs predominantly don't
391+
// use camel case.
392+
let has_repr_c = data.repr(self.db, enum_id).is_some_and(|repr| repr.c());
393+
if !has_repr_c {
394+
self.create_incorrect_case_diagnostic_for_item_name(
395+
enum_id,
396+
&data.name,
397+
CaseType::UpperCamelCase,
398+
IdentType::Enum,
399+
);
400+
}
390401

391402
// Check the variant names.
392403
self.validate_enum_variants(enum_id)

crates/ide-diagnostics/src/handlers/incorrect_case.rs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1059,4 +1059,19 @@ fn foo(_HelloWorld: ()) {}
10591059
"#,
10601060
);
10611061
}
1062+
1063+
#[test]
1064+
fn allow_with_repr_c() {
1065+
check_diagnostics(
1066+
r#"
1067+
#[repr(C)]
1068+
struct FFI_Struct;
1069+
1070+
#[repr(C)]
1071+
enum FFI_Enum {
1072+
Field,
1073+
}
1074+
"#,
1075+
);
1076+
}
10621077
}

crates/ide-diagnostics/src/lib.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,6 @@ use syntax::{
108108
ast::{self, AstNode},
109109
};
110110

111-
// FIXME: Make this an enum
112111
#[derive(Copy, Clone, Debug, PartialEq, Eq, Hash)]
113112
pub enum DiagnosticCode {
114113
RustcHardError(&'static str),

0 commit comments

Comments
 (0)