Skip to content

Add lint to detect tuple-like structs #22060

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions src/librustc/lint/builtin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2103,6 +2103,30 @@ impl LintPass for PrivateNoMangleFns {
}
}

#[derive(Copy)]
pub struct UsingTupleStructs;

declare_lint!(USING_TUPLE_STRUCTS, Allow,
"detects tuple-like structures");

impl LintPass for UsingTupleStructs {
fn get_lints(&self) -> LintArray {
lint_array!(USING_TUPLE_STRUCTS)
}

fn check_item(&mut self, cx: &Context, it: &ast::Item) {
if it.vis == ast::Visibility::Public {
if let ast::ItemStruct(ref def, _) = it.node {
if def.is_tuple_like() {
cx.span_lint(USING_TUPLE_STRUCTS, it.span,
"standard library should not use tuple-like structures; \
it hampers backward compatibility")
}
}
}
}
}

/// Forbids using the `#[feature(...)]` attribute
#[derive(Copy)]
pub struct UnstableFeatures;
Expand Down
1 change: 1 addition & 0 deletions src/librustc/lint/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -214,6 +214,7 @@ impl LintStore {
Stability,
UnconditionalRecursion,
PrivateNoMangleFns,
UsingTupleStructs,
);

add_builtin_with_new!(sess,
Expand Down
6 changes: 6 additions & 0 deletions src/libsyntax/ast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1596,6 +1596,12 @@ pub struct StructDef {
pub ctor_id: Option<NodeId>,
}

impl StructDef {
pub fn is_tuple_like(&self) -> bool {
self.fields.len() > 0 && self.fields[0].node.kind.is_unnamed()
}
}

/*
FIXME (#3300): Should allow items to be anonymous. Right now
we just use dummy names for anon items.
Expand Down
26 changes: 26 additions & 0 deletions src/test/compile-fail/lint-tuple-structs.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
// Copyright 2015 The Rust Project Developers. See the COPYRIGHT
// file at the top-level directory of this distribution and at
// http://rust-lang.org/COPYRIGHT.
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.

#![deny(using_tuple_structs)]
#![allow(dead_code)]

// regular struct is okay
pub struct S { x: usize, y: usize }

// enum-like struct is okay, too
pub struct ES;

// tuple-like struct is not
pub struct TS(usize); //~ ERROR standard library should not use tuple-like

// but non-public one is
struct TSPrivate(usize);

fn main() {}