Skip to content

Commit 8229241

Browse files
committed
Add lint to detect tuple-like structs
It is benign and could be activated by individual crates. cc #22045
1 parent ce5aad2 commit 8229241

File tree

4 files changed

+57
-0
lines changed

4 files changed

+57
-0
lines changed

src/librustc/lint/builtin.rs

+24
Original file line numberDiff line numberDiff line change
@@ -2103,6 +2103,30 @@ impl LintPass for PrivateNoMangleFns {
21032103
}
21042104
}
21052105

2106+
#[derive(Copy)]
2107+
pub struct UsingTupleStructs;
2108+
2109+
declare_lint!(USING_TUPLE_STRUCTS, Allow,
2110+
"detects tuple-like structures");
2111+
2112+
impl LintPass for UsingTupleStructs {
2113+
fn get_lints(&self) -> LintArray {
2114+
lint_array!(USING_TUPLE_STRUCTS)
2115+
}
2116+
2117+
fn check_item(&mut self, cx: &Context, it: &ast::Item) {
2118+
if it.vis == ast::Visibility::Public {
2119+
if let ast::ItemStruct(ref def, _) = it.node {
2120+
if def.is_tuple_like() {
2121+
cx.span_lint(USING_TUPLE_STRUCTS, it.span,
2122+
"standard library should not use tuple-like structures; \
2123+
it hampers backward compatibility")
2124+
}
2125+
}
2126+
}
2127+
}
2128+
}
2129+
21062130
/// Forbids using the `#[feature(...)]` attribute
21072131
#[derive(Copy)]
21082132
pub struct UnstableFeatures;

src/librustc/lint/context.rs

+1
Original file line numberDiff line numberDiff line change
@@ -214,6 +214,7 @@ impl LintStore {
214214
Stability,
215215
UnconditionalRecursion,
216216
PrivateNoMangleFns,
217+
UsingTupleStructs,
217218
);
218219

219220
add_builtin_with_new!(sess,

src/libsyntax/ast.rs

+6
Original file line numberDiff line numberDiff line change
@@ -1596,6 +1596,12 @@ pub struct StructDef {
15961596
pub ctor_id: Option<NodeId>,
15971597
}
15981598

1599+
impl StructDef {
1600+
pub fn is_tuple_like(&self) -> bool {
1601+
self.fields.len() > 0 && self.fields[0].node.kind.is_unnamed()
1602+
}
1603+
}
1604+
15991605
/*
16001606
FIXME (#3300): Should allow items to be anonymous. Right now
16011607
we just use dummy names for anon items.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
// Copyright 2015 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
#![deny(using_tuple_structs)]
12+
#![allow(dead_code)]
13+
14+
// regular struct is okay
15+
pub struct S { x: usize, y: usize }
16+
17+
// enum-like struct is okay, too
18+
pub struct ES;
19+
20+
// tuple-like struct is not
21+
pub struct TS(usize); //~ ERROR standard library should not use tuple-like
22+
23+
// but non-public one is
24+
struct TSPrivate(usize);
25+
26+
fn main() {}

0 commit comments

Comments
 (0)